Skip to content

Commit a509a28

Browse files
author
John J. Aylward
committed
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
1 parent 74b9a60 commit a509a28

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

JSONObject.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,16 @@ public JSONObject(Map<?, ?> m) {
291291
* Construct a JSONObject from an Object using bean getters. It reflects on
292292
* all of the public methods of the object. For each of the methods with no
293293
* parameters and a name starting with <code>"get"</code> or
294-
* <code>"is"</code>, the method is invoked, and a key and the value
295-
* returned from the getter method are put into the new JSONObject.
294+
* <code>"is"</code> followed by an uppercase letter, the method is invoked,
295+
* and a key and the value returned from the getter method are put into the
296+
* new JSONObject.
296297
* <p>
297298
* The key is formed by removing the <code>"get"</code> or <code>"is"</code>
298299
* prefix. If the second remaining character is not upper case, then the
299300
* first character is converted to lower case.
300301
* <p>
301-
* Methods that return <code>void</code> as well as <code>static</code>
302-
* methods are ignored.
302+
* Methods that are <code>static</code>, return <code>void</code>,
303+
* have parameters, or are "bridge" methods, are ignored.
303304
* <p>
304305
* For example, if an object has a method named <code>"getName"</code>, and
305306
* if the result of calling <code>object.getName()</code> is
@@ -315,6 +316,16 @@ public JSONObject(Map<?, ?> m) {
315316
* </pre>
316317
* The resulting JSON object would contain <code>"FullName": "Larry Fine"</code>
317318
* <p>
319+
* Similarly, the {@link JSONPropertyName} annotation can be used on non-
320+
* <code>get</code> and <code>is</code> methods. We can also override key
321+
* name used in the JSONObject as seen below even though the field would normally
322+
* be ignored:
323+
* <pre>
324+
* &#64;JSONPropertyName("FullName")
325+
* public String fullName() { return this.name; }
326+
* </pre>
327+
* The resulting JSON object would contain <code>"FullName": "Larry Fine"</code>
328+
* <p>
318329
* The {@link JSONPropertyIgnore} annotation can be used to force the bean property
319330
* to not be serialized into JSON. If both {@link JSONPropertyIgnore} and
320331
* {@link JSONPropertyName} are defined on the same method, a depth comparison is
@@ -1483,9 +1494,7 @@ && isValidMethodName(method.getName())) {
14831494
}
14841495

14851496
private boolean isValidMethodName(String name) {
1486-
return (name.startsWith("get") || name.startsWith("is"))
1487-
&& !"getClass".equals(name)
1488-
&& !"getDeclaringClass".equals(name);
1497+
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
14891498
}
14901499

14911500
private String getKeyNameFromMethod(Method method) {
@@ -1504,17 +1513,17 @@ private String getKeyNameFromMethod(Method method) {
15041513
}
15051514
String key;
15061515
final String name = method.getName();
1507-
if (name.startsWith("get")) {
1516+
if (name.startsWith("get") && name.length() > 3) {
15081517
key = name.substring(3);
1509-
} else if (name.startsWith("is")) {
1518+
} else if (name.startsWith("is") && name.length() > 2) {
15101519
key = name.substring(2);
15111520
} else {
15121521
return null;
15131522
}
15141523
// if the first letter in the key is not uppercase, then skip.
15151524
// This is to maintain backwards compatibility before PR406
15161525
// (https://github.com/stleary/JSON-java/pull/406/)
1517-
if(key.isEmpty() || Character.isLowerCase(key.charAt(0))) {
1526+
if (Character.isLowerCase(key.charAt(0))) {
15181527
return null;
15191528
}
15201529
if (key.length() == 1) {
@@ -1568,8 +1577,8 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
15681577
}
15691578

15701579
try {
1571-
return getAnnotation(m.getDeclaringClass().getSuperclass().getMethod(m.getName(),
1572-
m.getParameterTypes()),
1580+
return getAnnotation(
1581+
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
15731582
annotationClass);
15741583
} catch (final SecurityException ex) {
15751584
return null;
@@ -1626,8 +1635,7 @@ private static int getAnnotationDepth(final Method m, final Class<? extends Anno
16261635

16271636
try {
16281637
int d = getAnnotationDepth(
1629-
m.getDeclaringClass().getSuperclass().getMethod(m.getName(),
1630-
m.getParameterTypes()),
1638+
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
16311639
annotationClass);
16321640
if (d > 0) {
16331641
// since the annotation was on the superclass, add 1

0 commit comments

Comments
 (0)