@@ -61,7 +61,7 @@ of this software and associated documentation files (the "Software"), to deal
61
61
* coercion for you. The opt methods differ from the get methods in that they
62
62
* do not throw. Instead, they return a specified value, such as null.
63
63
* <p>
64
- * The <code>put</code> methods add or replace values in an object. For example,
64
+ * The <code>put</code> methods add or replace values in an object. For example,
65
65
* <pre>myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre>
66
66
* produces the string <code>{"JSON": "Hello, World"}</code>.
67
67
* <p>
@@ -86,7 +86,7 @@ of this software and associated documentation files (the "Software"), to deal
86
86
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
87
87
* </ul>
88
88
* @author JSON.org
89
- * @version 2011-04-05
89
+ * @version 2011-10-16
90
90
*/
91
91
public class JSONObject {
92
92
@@ -155,7 +155,7 @@ public JSONObject() {
155
155
* Missing keys are ignored.
156
156
* @param jo A JSONObject.
157
157
* @param names An array of strings.
158
- * @throws JSONException
158
+ * @throws JSONException
159
159
* @exception JSONException If a value is a non-finite number or if a name is duplicated.
160
160
*/
161
161
public JSONObject (JSONObject jo , String [] names ) {
@@ -231,7 +231,7 @@ public JSONObject(JSONTokener x) throws JSONException {
231
231
*
232
232
* @param map A map object that can be used to initialize the contents of
233
233
* the JSONObject.
234
- * @throws JSONException
234
+ * @throws JSONException
235
235
*/
236
236
public JSONObject (Map map ) {
237
237
this .map = new HashMap ();
@@ -319,20 +319,20 @@ public JSONObject(String source) throws JSONException {
319
319
*/
320
320
public JSONObject (String baseName , Locale locale ) throws JSONException {
321
321
this ();
322
- ResourceBundle bundle = ResourceBundle .getBundle (baseName , locale ,
322
+ ResourceBundle bundle = ResourceBundle .getBundle (baseName , locale ,
323
323
Thread .currentThread ().getContextClassLoader ());
324
324
325
325
// Iterate through the keys in the bundle.
326
-
326
+
327
327
Enumeration keys = bundle .getKeys ();
328
328
while (keys .hasMoreElements ()) {
329
329
Object key = keys .nextElement ();
330
330
if (key instanceof String ) {
331
-
332
- // Go through the path, ensuring that there is a nested JSONObject for each
331
+
332
+ // Go through the path, ensuring that there is a nested JSONObject for each
333
333
// segment except the last. Add the value using the last segment's name into
334
334
// the deepest nested JSONObject.
335
-
335
+
336
336
String [] path = ((String )key ).split ("\\ ." );
337
337
int last = path .length - 1 ;
338
338
JSONObject target = this ;
@@ -350,16 +350,16 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
350
350
}
351
351
}
352
352
353
-
353
+
354
354
/**
355
355
* Accumulate values under a key. It is similar to the put method except
356
356
* that if there is already an object stored under the key then a
357
357
* JSONArray is stored under the key to hold all of the accumulated values.
358
358
* If there is already a JSONArray, then the new value is appended to it.
359
359
* In contrast, the put method replaces the previous value.
360
- *
360
+ *
361
361
* If only one value is accumulated that is not a JSONArray, then the
362
- * result will be the same as using put. But if multiple values are
362
+ * result will be the same as using put. But if multiple values are
363
363
* accumulated, then the result will be like append.
364
364
* @param key A key string.
365
365
* @param value An object to be accumulated under the key.
@@ -368,7 +368,7 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
368
368
* or if the key is null.
369
369
*/
370
370
public JSONObject accumulate (
371
- String key ,
371
+ String key ,
372
372
Object value
373
373
) throws JSONException {
374
374
testValidity (value );
@@ -425,8 +425,8 @@ public static String doubleToString(double d) {
425
425
// Shave off trailing zeros and decimal point, if possible.
426
426
427
427
String string = Double .toString (d );
428
- if (string .indexOf ('.' ) > 0 && string .indexOf ('e' ) < 0 &&
429
- string .indexOf ('E' ) < 0 ) {
428
+ if (string .indexOf ('.' ) > 0 && string .indexOf ('e' ) < 0 &&
429
+ string .indexOf ('E' ) < 0 ) {
430
430
while (string .endsWith ("0" )) {
431
431
string = string .substring (0 , string .length () - 1 );
432
432
}
@@ -503,7 +503,7 @@ public double getDouble(String key) throws JSONException {
503
503
504
504
505
505
/**
506
- * Get the int value associated with a key.
506
+ * Get the int value associated with a key.
507
507
*
508
508
* @param key A key string.
509
509
* @return The integer value.
@@ -560,7 +560,7 @@ public JSONObject getJSONObject(String key) throws JSONException {
560
560
561
561
562
562
/**
563
- * Get the long value associated with a key.
563
+ * Get the long value associated with a key.
564
564
*
565
565
* @param key A key string.
566
566
* @return The long value.
@@ -649,8 +649,8 @@ public String getString(String key) throws JSONException {
649
649
public boolean has (String key ) {
650
650
return this .map .containsKey (key );
651
651
}
652
-
653
-
652
+
653
+
654
654
/**
655
655
* Increment a property of a JSONObject. If there is no such property,
656
656
* create one with a value of 1. If there is such a property, and if
@@ -667,11 +667,11 @@ public JSONObject increment(String key) throws JSONException {
667
667
} else if (value instanceof Integer ) {
668
668
put (key , ((Integer )value ).intValue () + 1 );
669
669
} else if (value instanceof Long ) {
670
- put (key , ((Long )value ).longValue () + 1 );
670
+ put (key , ((Long )value ).longValue () + 1 );
671
671
} else if (value instanceof Double ) {
672
- put (key , ((Double )value ).doubleValue () + 1 );
672
+ put (key , ((Double )value ).doubleValue () + 1 );
673
673
} else if (value instanceof Float ) {
674
- put (key , ((Float )value ).floatValue () + 1 );
674
+ put (key , ((Float )value ).floatValue () + 1 );
675
675
} else {
676
676
throw new JSONException ("Unable to increment [" + quote (key ) + "]." );
677
677
}
@@ -742,8 +742,8 @@ public static String numberToString(Number number)
742
742
// Shave off trailing zeros and decimal point, if possible.
743
743
744
744
String string = number .toString ();
745
- if (string .indexOf ('.' ) > 0 && string .indexOf ('e' ) < 0 &&
746
- string .indexOf ('E' ) < 0 ) {
745
+ if (string .indexOf ('.' ) > 0 && string .indexOf ('e' ) < 0 &&
746
+ string .indexOf ('E' ) < 0 ) {
747
747
while (string .endsWith ("0" )) {
748
748
string = string .substring (0 , string .length () - 1 );
749
749
}
@@ -946,14 +946,14 @@ public String optString(String key) {
946
946
*/
947
947
public String optString (String key , String defaultValue ) {
948
948
Object object = opt (key );
949
- return NULL .equals (object ) ? defaultValue : object .toString ();
949
+ return NULL .equals (object ) ? defaultValue : object .toString ();
950
950
}
951
951
952
952
953
953
private void populateMap (Object bean ) {
954
954
Class klass = bean .getClass ();
955
955
956
- // If klass is a System class then set includeSuperClass to false.
956
+ // If klass is a System class then set includeSuperClass to false.
957
957
958
958
boolean includeSuperClass = klass .getClassLoader () != null ;
959
959
@@ -966,7 +966,7 @@ private void populateMap(Object bean) {
966
966
String name = method .getName ();
967
967
String key = "" ;
968
968
if (name .startsWith ("get" )) {
969
- if (name .equals ("getClass" ) ||
969
+ if (name .equals ("getClass" ) ||
970
970
name .equals ("getDeclaringClass" )) {
971
971
key = "" ;
972
972
} else {
@@ -1147,7 +1147,7 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
1147
1147
/**
1148
1148
* Produce a string in double quotes with backslash sequences in all the
1149
1149
* right places. A backslash will be inserted within </, producing <\/,
1150
- * allowing JSON text to be delivered in HTML. In JSON text, a string
1150
+ * allowing JSON text to be delivered in HTML. In JSON text, a string
1151
1151
* cannot contain a control character or an unescaped quote or backslash.
1152
1152
* @param string A String
1153
1153
* @return A String correctly formatted for insertion in a JSON text.
@@ -1226,6 +1226,7 @@ public Object remove(String key) {
1226
1226
* @return A simple JSON value.
1227
1227
*/
1228
1228
public static Object stringToValue (String string ) {
1229
+ Double d ;
1229
1230
if (string .equals ("" )) {
1230
1231
return string ;
1231
1232
}
@@ -1240,8 +1241,8 @@ public static Object stringToValue(String string) {
1240
1241
}
1241
1242
1242
1243
/*
1243
- * If it might be a number, try converting it.
1244
- * We support the non-standard 0x- convention.
1244
+ * If it might be a number, try converting it.
1245
+ * We support the non-standard 0x- convention.
1245
1246
* If a number cannot be produced, then the value will just
1246
1247
* be a string. Note that the 0x-, plus, and implied string
1247
1248
* conventions are non-standard. A JSON parser may accept
@@ -1258,9 +1259,12 @@ public static Object stringToValue(String string) {
1258
1259
}
1259
1260
}
1260
1261
try {
1261
- if (string .indexOf ('.' ) > -1 ||
1262
+ if (string .indexOf ('.' ) > -1 ||
1262
1263
string .indexOf ('e' ) > -1 || string .indexOf ('E' ) > -1 ) {
1263
- return Double .valueOf (string );
1264
+ d = Double .valueOf (string );
1265
+ if (!d .isInfinite () && !d .isNaN ()) {
1266
+ return d ;
1267
+ }
1264
1268
} else {
1265
1269
Long myLong = new Long (string );
1266
1270
if (myLong .longValue () == myLong .intValue ()) {
@@ -1497,8 +1501,8 @@ public static String valueToString(Object value) throws JSONException {
1497
1501
* @throws JSONException If the object contains an invalid number.
1498
1502
*/
1499
1503
static String valueToString (
1500
- Object value ,
1501
- int indentFactor ,
1504
+ Object value ,
1505
+ int indentFactor ,
1502
1506
int indent
1503
1507
) throws JSONException {
1504
1508
if (value == null || value .equals (null )) {
@@ -1539,11 +1543,11 @@ static String valueToString(
1539
1543
1540
1544
1541
1545
/**
1542
- * Wrap an object, if necessary. If the object is null, return the NULL
1543
- * object. If it is an array or collection, wrap it in a JSONArray. If
1544
- * it is a map, wrap it in a JSONObject. If it is a standard property
1545
- * (Double, String, et al) then it is already wrapped. Otherwise, if it
1546
- * comes from one of the java packages, turn it into a string. And if
1546
+ * Wrap an object, if necessary. If the object is null, return the NULL
1547
+ * object. If it is an array or collection, wrap it in a JSONArray. If
1548
+ * it is a map, wrap it in a JSONObject. If it is a standard property
1549
+ * (Double, String, et al) then it is already wrapped. Otherwise, if it
1550
+ * comes from one of the java packages, turn it into a string. And if
1547
1551
* it doesn't, try to wrap it in a JSONObject. If the wrapping fails,
1548
1552
* then null is returned.
1549
1553
*
@@ -1555,16 +1559,16 @@ public static Object wrap(Object object) {
1555
1559
if (object == null ) {
1556
1560
return NULL ;
1557
1561
}
1558
- if (object instanceof JSONObject || object instanceof JSONArray ||
1559
- NULL .equals (object ) || object instanceof JSONString ||
1562
+ if (object instanceof JSONObject || object instanceof JSONArray ||
1563
+ NULL .equals (object ) || object instanceof JSONString ||
1560
1564
object instanceof Byte || object instanceof Character ||
1561
1565
object instanceof Short || object instanceof Integer ||
1562
- object instanceof Long || object instanceof Boolean ||
1566
+ object instanceof Long || object instanceof Boolean ||
1563
1567
object instanceof Float || object instanceof Double ||
1564
1568
object instanceof String ) {
1565
1569
return object ;
1566
1570
}
1567
-
1571
+
1568
1572
if (object instanceof Collection ) {
1569
1573
return new JSONArray ((Collection )object );
1570
1574
}
@@ -1575,7 +1579,7 @@ public static Object wrap(Object object) {
1575
1579
return new JSONObject ((Map )object );
1576
1580
}
1577
1581
Package objectPackage = object .getClass ().getPackage ();
1578
- String objectPackageName = objectPackage != null ?
1582
+ String objectPackageName = objectPackage != null ?
1579
1583
objectPackage .getName () : "" ;
1580
1584
if (
1581
1585
objectPackageName .startsWith ("java." ) ||
@@ -1590,7 +1594,7 @@ public static Object wrap(Object object) {
1590
1594
}
1591
1595
}
1592
1596
1593
-
1597
+
1594
1598
/**
1595
1599
* Write the contents of the JSONObject as JSON text to a writer.
1596
1600
* For compactness, no whitespace is added.
0 commit comments