@@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
3131 * This provides static methods to convert an XML text into a JSONObject,
3232 * and to covert a JSONObject into an XML text.
3333 * @author JSON.org
34- * @version 2012-10-26
34+ * @version 2013-11-12
3535 */
3636public class XML {
3737
@@ -301,9 +301,6 @@ private static boolean parse(XMLTokener x, JSONObject context,
301301 * @return A simple JSON value.
302302 */
303303 public static Object stringToValue (String string ) {
304- if ("" .equals (string )) {
305- return string ;
306- }
307304 if ("true" .equalsIgnoreCase (string )) {
308305 return Boolean .TRUE ;
309306 }
@@ -313,36 +310,26 @@ public static Object stringToValue(String string) {
313310 if ("null" .equalsIgnoreCase (string )) {
314311 return JSONObject .NULL ;
315312 }
316- if ("0" .equals (string )) {
317- return new Integer (0 );
318- }
319313
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.
322316
323317 try {
324318 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 ;
343323 }
344324 }
345325 } 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+ }
346333 }
347334 return string ;
348335 }
0 commit comments