@@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
31
31
* This provides static methods to convert an XML text into a JSONObject,
32
32
* and to covert a JSONObject into an XML text.
33
33
* @author JSON.org
34
- * @version 2012-10-26
34
+ * @version 2013-11-12
35
35
*/
36
36
public class XML {
37
37
@@ -301,9 +301,6 @@ private static boolean parse(XMLTokener x, JSONObject context,
301
301
* @return A simple JSON value.
302
302
*/
303
303
public static Object stringToValue (String string ) {
304
- if ("" .equals (string )) {
305
- return string ;
306
- }
307
304
if ("true" .equalsIgnoreCase (string )) {
308
305
return Boolean .TRUE ;
309
306
}
@@ -313,36 +310,26 @@ public static Object stringToValue(String string) {
313
310
if ("null" .equalsIgnoreCase (string )) {
314
311
return JSONObject .NULL ;
315
312
}
316
- if ("0" .equals (string )) {
317
- return new Integer (0 );
318
- }
319
313
320
- // If it might be a number, try converting it. If that doesn't work,
321
- // return the string.
314
+ // If it might be a number, try converting it, first as a Long, and then as a
315
+ // Double. If that doesn't work, return the string.
322
316
323
317
try {
324
318
char initial = string .charAt (0 );
325
- boolean negative = false ;
326
- if (initial == '-' ) {
327
- initial = string .charAt (1 );
328
- negative = true ;
329
- }
330
- if (initial == '0' && string .charAt (negative ? 2 : 1 ) == '0' ) {
331
- return string ;
332
- }
333
- if ((initial >= '0' && initial <= '9' )) {
334
- if (string .indexOf ('.' ) >= 0 ) {
335
- return Double .valueOf (string );
336
- } else if (string .indexOf ('e' ) < 0 && string .indexOf ('E' ) < 0 ) {
337
- Long myLong = new Long (string );
338
- if (myLong .longValue () == myLong .intValue ()) {
339
- return new Integer (myLong .intValue ());
340
- } else {
341
- return myLong ;
342
- }
319
+ if (initial == '-' || (initial >= '0' && initial <= '9' )) {
320
+ Long value = new Long (string );
321
+ if (value .toString ().equals (string )) {
322
+ return value ;
343
323
}
344
324
}
345
325
} catch (Exception ignore ) {
326
+ try {
327
+ Double value = new Double (string );
328
+ if (value .toString ().equals (string )) {
329
+ return value ;
330
+ }
331
+ } catch (Exception ignoreAlso ) {
332
+ }
346
333
}
347
334
return string ;
348
335
}
0 commit comments