mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Cleans up the name check a little to be more permissive on what can be tagged with the new JSONPropertyName annotation.
Also updates the javadoc to reflect the new name allowances
This commit is contained in:
parent
74b9a60f98
commit
a509a28ed4
1 changed files with 22 additions and 14 deletions
|
@ -291,15 +291,16 @@ public class JSONObject {
|
||||||
* Construct a JSONObject from an Object using bean getters. It reflects on
|
* Construct a JSONObject from an Object using bean getters. It reflects on
|
||||||
* all of the public methods of the object. For each of the methods with no
|
* all of the public methods of the object. For each of the methods with no
|
||||||
* parameters and a name starting with <code>"get"</code> or
|
* parameters and a name starting with <code>"get"</code> or
|
||||||
* <code>"is"</code>, the method is invoked, and a key and the value
|
* <code>"is"</code> followed by an uppercase letter, the method is invoked,
|
||||||
* returned from the getter method are put into the new JSONObject.
|
* and a key and the value returned from the getter method are put into the
|
||||||
|
* new JSONObject.
|
||||||
* <p>
|
* <p>
|
||||||
* The key is formed by removing the <code>"get"</code> or <code>"is"</code>
|
* The key is formed by removing the <code>"get"</code> or <code>"is"</code>
|
||||||
* prefix. If the second remaining character is not upper case, then the
|
* prefix. If the second remaining character is not upper case, then the
|
||||||
* first character is converted to lower case.
|
* first character is converted to lower case.
|
||||||
* <p>
|
* <p>
|
||||||
* Methods that return <code>void</code> as well as <code>static</code>
|
* Methods that are <code>static</code>, return <code>void</code>,
|
||||||
* methods are ignored.
|
* have parameters, or are "bridge" methods, are ignored.
|
||||||
* <p>
|
* <p>
|
||||||
* For example, if an object has a method named <code>"getName"</code>, and
|
* For example, if an object has a method named <code>"getName"</code>, and
|
||||||
* if the result of calling <code>object.getName()</code> is
|
* if the result of calling <code>object.getName()</code> is
|
||||||
|
@ -315,6 +316,16 @@ public class JSONObject {
|
||||||
* </pre>
|
* </pre>
|
||||||
* The resulting JSON object would contain <code>"FullName": "Larry Fine"</code>
|
* The resulting JSON object would contain <code>"FullName": "Larry Fine"</code>
|
||||||
* <p>
|
* <p>
|
||||||
|
* Similarly, the {@link JSONPropertyName} annotation can be used on non-
|
||||||
|
* <code>get</code> and <code>is</code> methods. We can also override key
|
||||||
|
* name used in the JSONObject as seen below even though the field would normally
|
||||||
|
* be ignored:
|
||||||
|
* <pre>
|
||||||
|
* @JSONPropertyName("FullName")
|
||||||
|
* public String fullName() { return this.name; }
|
||||||
|
* </pre>
|
||||||
|
* The resulting JSON object would contain <code>"FullName": "Larry Fine"</code>
|
||||||
|
* <p>
|
||||||
* The {@link JSONPropertyIgnore} annotation can be used to force the bean property
|
* The {@link JSONPropertyIgnore} annotation can be used to force the bean property
|
||||||
* to not be serialized into JSON. If both {@link JSONPropertyIgnore} and
|
* to not be serialized into JSON. If both {@link JSONPropertyIgnore} and
|
||||||
* {@link JSONPropertyName} are defined on the same method, a depth comparison is
|
* {@link JSONPropertyName} are defined on the same method, a depth comparison is
|
||||||
|
@ -1483,9 +1494,7 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidMethodName(String name) {
|
private boolean isValidMethodName(String name) {
|
||||||
return (name.startsWith("get") || name.startsWith("is"))
|
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
|
||||||
&& !"getClass".equals(name)
|
|
||||||
&& !"getDeclaringClass".equals(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getKeyNameFromMethod(Method method) {
|
private String getKeyNameFromMethod(Method method) {
|
||||||
|
@ -1504,9 +1513,9 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
String key;
|
String key;
|
||||||
final String name = method.getName();
|
final String name = method.getName();
|
||||||
if (name.startsWith("get")) {
|
if (name.startsWith("get") && name.length() > 3) {
|
||||||
key = name.substring(3);
|
key = name.substring(3);
|
||||||
} else if (name.startsWith("is")) {
|
} else if (name.startsWith("is") && name.length() > 2) {
|
||||||
key = name.substring(2);
|
key = name.substring(2);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -1514,7 +1523,7 @@ public class JSONObject {
|
||||||
// if the first letter in the key is not uppercase, then skip.
|
// if the first letter in the key is not uppercase, then skip.
|
||||||
// This is to maintain backwards compatibility before PR406
|
// This is to maintain backwards compatibility before PR406
|
||||||
// (https://github.com/stleary/JSON-java/pull/406/)
|
// (https://github.com/stleary/JSON-java/pull/406/)
|
||||||
if(key.isEmpty() || Character.isLowerCase(key.charAt(0))) {
|
if (Character.isLowerCase(key.charAt(0))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (key.length() == 1) {
|
if (key.length() == 1) {
|
||||||
|
@ -1568,8 +1577,8 @@ public class JSONObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return getAnnotation(m.getDeclaringClass().getSuperclass().getMethod(m.getName(),
|
return getAnnotation(
|
||||||
m.getParameterTypes()),
|
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
|
||||||
annotationClass);
|
annotationClass);
|
||||||
} catch (final SecurityException ex) {
|
} catch (final SecurityException ex) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -1626,8 +1635,7 @@ public class JSONObject {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int d = getAnnotationDepth(
|
int d = getAnnotationDepth(
|
||||||
m.getDeclaringClass().getSuperclass().getMethod(m.getName(),
|
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
|
||||||
m.getParameterTypes()),
|
|
||||||
annotationClass);
|
annotationClass);
|
||||||
if (d > 0) {
|
if (d > 0) {
|
||||||
// since the annotation was on the superclass, add 1
|
// since the annotation was on the superclass, add 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue