Skip to content

Commit 88f27f3

Browse files
non-number numbers
1 parent 3e3951f commit 88f27f3

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

JSONObject.java

+49-45
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ of this software and associated documentation files (the "Software"), to deal
6161
* coercion for you. The opt methods differ from the get methods in that they
6262
* do not throw. Instead, they return a specified value, such as null.
6363
* <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,
6565
* <pre>myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre>
6666
* produces the string <code>{"JSON": "Hello, World"}</code>.
6767
* <p>
@@ -86,7 +86,7 @@ of this software and associated documentation files (the "Software"), to deal
8686
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
8787
* </ul>
8888
* @author JSON.org
89-
* @version 2011-04-05
89+
* @version 2011-10-16
9090
*/
9191
public class JSONObject {
9292

@@ -155,7 +155,7 @@ public JSONObject() {
155155
* Missing keys are ignored.
156156
* @param jo A JSONObject.
157157
* @param names An array of strings.
158-
* @throws JSONException
158+
* @throws JSONException
159159
* @exception JSONException If a value is a non-finite number or if a name is duplicated.
160160
*/
161161
public JSONObject(JSONObject jo, String[] names) {
@@ -231,7 +231,7 @@ public JSONObject(JSONTokener x) throws JSONException {
231231
*
232232
* @param map A map object that can be used to initialize the contents of
233233
* the JSONObject.
234-
* @throws JSONException
234+
* @throws JSONException
235235
*/
236236
public JSONObject(Map map) {
237237
this.map = new HashMap();
@@ -319,20 +319,20 @@ public JSONObject(String source) throws JSONException {
319319
*/
320320
public JSONObject(String baseName, Locale locale) throws JSONException {
321321
this();
322-
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
322+
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
323323
Thread.currentThread().getContextClassLoader());
324324

325325
// Iterate through the keys in the bundle.
326-
326+
327327
Enumeration keys = bundle.getKeys();
328328
while (keys.hasMoreElements()) {
329329
Object key = keys.nextElement();
330330
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
333333
// segment except the last. Add the value using the last segment's name into
334334
// the deepest nested JSONObject.
335-
335+
336336
String[] path = ((String)key).split("\\.");
337337
int last = path.length - 1;
338338
JSONObject target = this;
@@ -350,16 +350,16 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
350350
}
351351
}
352352

353-
353+
354354
/**
355355
* Accumulate values under a key. It is similar to the put method except
356356
* that if there is already an object stored under the key then a
357357
* JSONArray is stored under the key to hold all of the accumulated values.
358358
* If there is already a JSONArray, then the new value is appended to it.
359359
* In contrast, the put method replaces the previous value.
360-
*
360+
*
361361
* 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
363363
* accumulated, then the result will be like append.
364364
* @param key A key string.
365365
* @param value An object to be accumulated under the key.
@@ -368,7 +368,7 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
368368
* or if the key is null.
369369
*/
370370
public JSONObject accumulate(
371-
String key,
371+
String key,
372372
Object value
373373
) throws JSONException {
374374
testValidity(value);
@@ -425,8 +425,8 @@ public static String doubleToString(double d) {
425425
// Shave off trailing zeros and decimal point, if possible.
426426

427427
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) {
430430
while (string.endsWith("0")) {
431431
string = string.substring(0, string.length() - 1);
432432
}
@@ -503,7 +503,7 @@ public double getDouble(String key) throws JSONException {
503503

504504

505505
/**
506-
* Get the int value associated with a key.
506+
* Get the int value associated with a key.
507507
*
508508
* @param key A key string.
509509
* @return The integer value.
@@ -560,7 +560,7 @@ public JSONObject getJSONObject(String key) throws JSONException {
560560

561561

562562
/**
563-
* Get the long value associated with a key.
563+
* Get the long value associated with a key.
564564
*
565565
* @param key A key string.
566566
* @return The long value.
@@ -649,8 +649,8 @@ public String getString(String key) throws JSONException {
649649
public boolean has(String key) {
650650
return this.map.containsKey(key);
651651
}
652-
653-
652+
653+
654654
/**
655655
* Increment a property of a JSONObject. If there is no such property,
656656
* 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 {
667667
} else if (value instanceof Integer) {
668668
put(key, ((Integer)value).intValue() + 1);
669669
} else if (value instanceof Long) {
670-
put(key, ((Long)value).longValue() + 1);
670+
put(key, ((Long)value).longValue() + 1);
671671
} else if (value instanceof Double) {
672-
put(key, ((Double)value).doubleValue() + 1);
672+
put(key, ((Double)value).doubleValue() + 1);
673673
} else if (value instanceof Float) {
674-
put(key, ((Float)value).floatValue() + 1);
674+
put(key, ((Float)value).floatValue() + 1);
675675
} else {
676676
throw new JSONException("Unable to increment [" + quote(key) + "].");
677677
}
@@ -742,8 +742,8 @@ public static String numberToString(Number number)
742742
// Shave off trailing zeros and decimal point, if possible.
743743

744744
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) {
747747
while (string.endsWith("0")) {
748748
string = string.substring(0, string.length() - 1);
749749
}
@@ -946,14 +946,14 @@ public String optString(String key) {
946946
*/
947947
public String optString(String key, String defaultValue) {
948948
Object object = opt(key);
949-
return NULL.equals(object) ? defaultValue : object.toString();
949+
return NULL.equals(object) ? defaultValue : object.toString();
950950
}
951951

952952

953953
private void populateMap(Object bean) {
954954
Class klass = bean.getClass();
955955

956-
// If klass is a System class then set includeSuperClass to false.
956+
// If klass is a System class then set includeSuperClass to false.
957957

958958
boolean includeSuperClass = klass.getClassLoader() != null;
959959

@@ -966,7 +966,7 @@ private void populateMap(Object bean) {
966966
String name = method.getName();
967967
String key = "";
968968
if (name.startsWith("get")) {
969-
if (name.equals("getClass") ||
969+
if (name.equals("getClass") ||
970970
name.equals("getDeclaringClass")) {
971971
key = "";
972972
} else {
@@ -1147,7 +1147,7 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
11471147
/**
11481148
* Produce a string in double quotes with backslash sequences in all the
11491149
* 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
11511151
* cannot contain a control character or an unescaped quote or backslash.
11521152
* @param string A String
11531153
* @return A String correctly formatted for insertion in a JSON text.
@@ -1226,6 +1226,7 @@ public Object remove(String key) {
12261226
* @return A simple JSON value.
12271227
*/
12281228
public static Object stringToValue(String string) {
1229+
Double d;
12291230
if (string.equals("")) {
12301231
return string;
12311232
}
@@ -1240,8 +1241,8 @@ public static Object stringToValue(String string) {
12401241
}
12411242

12421243
/*
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.
12451246
* If a number cannot be produced, then the value will just
12461247
* be a string. Note that the 0x-, plus, and implied string
12471248
* conventions are non-standard. A JSON parser may accept
@@ -1258,9 +1259,12 @@ public static Object stringToValue(String string) {
12581259
}
12591260
}
12601261
try {
1261-
if (string.indexOf('.') > -1 ||
1262+
if (string.indexOf('.') > -1 ||
12621263
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+
}
12641268
} else {
12651269
Long myLong = new Long(string);
12661270
if (myLong.longValue() == myLong.intValue()) {
@@ -1497,8 +1501,8 @@ public static String valueToString(Object value) throws JSONException {
14971501
* @throws JSONException If the object contains an invalid number.
14981502
*/
14991503
static String valueToString(
1500-
Object value,
1501-
int indentFactor,
1504+
Object value,
1505+
int indentFactor,
15021506
int indent
15031507
) throws JSONException {
15041508
if (value == null || value.equals(null)) {
@@ -1539,11 +1543,11 @@ static String valueToString(
15391543

15401544

15411545
/**
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
15471551
* it doesn't, try to wrap it in a JSONObject. If the wrapping fails,
15481552
* then null is returned.
15491553
*
@@ -1555,16 +1559,16 @@ public static Object wrap(Object object) {
15551559
if (object == null) {
15561560
return NULL;
15571561
}
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 ||
15601564
object instanceof Byte || object instanceof Character ||
15611565
object instanceof Short || object instanceof Integer ||
1562-
object instanceof Long || object instanceof Boolean ||
1566+
object instanceof Long || object instanceof Boolean ||
15631567
object instanceof Float || object instanceof Double ||
15641568
object instanceof String) {
15651569
return object;
15661570
}
1567-
1571+
15681572
if (object instanceof Collection) {
15691573
return new JSONArray((Collection)object);
15701574
}
@@ -1575,7 +1579,7 @@ public static Object wrap(Object object) {
15751579
return new JSONObject((Map)object);
15761580
}
15771581
Package objectPackage = object.getClass().getPackage();
1578-
String objectPackageName = objectPackage != null ?
1582+
String objectPackageName = objectPackage != null ?
15791583
objectPackage.getName() : "";
15801584
if (
15811585
objectPackageName.startsWith("java.") ||
@@ -1590,7 +1594,7 @@ public static Object wrap(Object object) {
15901594
}
15911595
}
15921596

1593-
1597+
15941598
/**
15951599
* Write the contents of the JSONObject as JSON text to a writer.
15961600
* For compactness, no whitespace is added.

Test.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ of this software and associated documentation files (the "Software"), to deal
4040
* comparisons of .toString to a string literal are likely to fail.
4141
*
4242
* @author JSON.org
43-
* @version 2011-10-05
43+
* @version 2011-10-16
4444
*/
4545
public class Test extends TestCase {
4646
public Test(String name) {
@@ -67,6 +67,10 @@ public void testXML() throws Exception {
6767
jsonobject = XML.toJSONObject(string);
6868
assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}", jsonobject.toString(2));
6969
assertEquals("<test><blank/><empty/></test>", XML.toString(jsonobject));
70+
71+
string = "<subsonic-response><playlists><playlist id=\"476c65652e6d3375\"/><playlist id=\"50617274792e78737066\"/></playlists></subsonic-response>";
72+
jsonobject = XML.toJSONObject(string);
73+
assertEquals("{\"subsonic-response\":\"playlists\":{\"playlist\":[{\"id\":\"476c65652e6d337\"},\"id\":\"50617274792e78737066\"}]}}}", jsonobject.toString());
7074
}
7175

7276
public void testNull() throws Exception {
@@ -95,6 +99,10 @@ public void testJSON() throws Exception {
9599
jsonarray = new JSONArray(string);
96100
assertEquals("[1122334455]", jsonarray.toString());
97101

102+
string = "[666e666]";
103+
jsonarray = new JSONArray(string);
104+
assertEquals("[\"666e666\"]", jsonarray.toString());
105+
98106
string = "[00.10]";
99107
jsonarray = new JSONArray(string);
100108
assertEquals("[0.1]", jsonarray.toString());

0 commit comments

Comments
 (0)