Skip to content

Commit 3007fc8

Browse files
author
John J. Aylward
committed
Removes custom XML stringToValue method in favor of keeping a consistent
implementation in JSONObject
1 parent 39b1c0c commit 3007fc8

File tree

3 files changed

+7
-52
lines changed

3 files changed

+7
-52
lines changed

JSONML.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static Object parse(
174174
if (!(token instanceof String)) {
175175
throw x.syntaxError("Missing value");
176176
}
177-
newjo.accumulate(attribute, XML.stringToValue((String)token));
177+
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
178178
token = null;
179179
} else {
180180
newjo.accumulate(attribute, "");
@@ -227,7 +227,7 @@ private static Object parse(
227227
} else {
228228
if (ja != null) {
229229
ja.put(token instanceof String
230-
? XML.stringToValue((String)token)
230+
? JSONObject.stringToValue((String)token)
231231
: token);
232232
}
233233
}

JSONObject.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,6 @@ public boolean similar(Object other) {
14781478
* @return A simple JSON value.
14791479
*/
14801480
public static Object stringToValue(String string) {
1481-
Double d;
14821481
if (string.equals("")) {
14831482
return string;
14841483
}
@@ -1497,13 +1496,13 @@ public static Object stringToValue(String string) {
14971496
* produced, then the value will just be a string.
14981497
*/
14991498

1500-
char b = string.charAt(0);
1501-
if ((b >= '0' && b <= '9') || b == '-') {
1499+
char initial = string.charAt(0);
1500+
if ((initial >= '0' && initial <= '9') || initial == '-') {
15021501
try {
15031502
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
15041503
|| string.indexOf('E') > -1
15051504
|| "-0".equals(string)) {
1506-
d = Double.valueOf(string);
1505+
Double d = Double.valueOf(string);
15071506
if (!d.isInfinite() && !d.isNaN()) {
15081507
return d;
15091508
}

XML.java

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
238238
throw x.syntaxError("Missing value");
239239
}
240240
jsonobject.accumulate(string,
241-
XML.stringToValue((String) token));
241+
JSONObject.stringToValue((String) token));
242242
token = null;
243243
} else {
244244
jsonobject.accumulate(string, "");
@@ -270,7 +270,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
270270
string = (String) token;
271271
if (string.length() > 0) {
272272
jsonobject.accumulate("content",
273-
XML.stringToValue(string));
273+
JSONObject.stringToValue(string));
274274
}
275275

276276
} else if (token == LT) {
@@ -296,50 +296,6 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
296296
}
297297
}
298298

299-
/**
300-
* Try to convert a string into a number, boolean, or null. If the string
301-
* can't be converted, return the string. This is much less ambitious than
302-
* JSONObject.stringToValue, especially because it does not attempt to
303-
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
304-
* points.
305-
*
306-
* @param string
307-
* A String.
308-
* @return A simple JSON value.
309-
*/
310-
public static Object stringToValue(String string) {
311-
if ("true".equalsIgnoreCase(string)) {
312-
return Boolean.TRUE;
313-
}
314-
if ("false".equalsIgnoreCase(string)) {
315-
return Boolean.FALSE;
316-
}
317-
if ("null".equalsIgnoreCase(string)) {
318-
return JSONObject.NULL;
319-
}
320-
321-
// If it might be a number, try converting it, first as a Long, and then
322-
// as a Double. If that doesn't work, return the string.
323-
try {
324-
char initial = string.charAt(0);
325-
if (initial == '-' || (initial >= '0' && initial <= '9')) {
326-
Long value = new Long(string);
327-
if (value.toString().equals(string)) {
328-
return value;
329-
}
330-
}
331-
} catch (Exception ignore) {
332-
try {
333-
Double value = new Double(string);
334-
if (value.toString().equals(string)) {
335-
return value;
336-
}
337-
} catch (Exception ignoreAlso) {
338-
}
339-
}
340-
return string;
341-
}
342-
343299
/**
344300
* Convert a well-formed (but not necessarily valid) XML string into a
345301
* JSONObject. Some information may be lost in this transformation because

0 commit comments

Comments
 (0)