From 07b2d65e30c4a7ffa789360c2f13e5eb5e417339 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 10:39:31 -0500 Subject: [PATCH 1/4] Fixes #187 -0 now returns as a double. --- JSONObject.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 2f613f855..7b71f37b3 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -30,7 +30,8 @@ of this software and associated documentation files (the "Software"), to deal import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.math.*; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; @@ -1500,7 +1501,8 @@ public static Object stringToValue(String string) { if ((b >= '0' && b <= '9') || b == '-') { try { if (string.indexOf('.') > -1 || string.indexOf('e') > -1 - || string.indexOf('E') > -1) { + || string.indexOf('E') > -1 + || "0".equals(string.substring(1))) { d = Double.valueOf(string); if (!d.isInfinite() && !d.isNaN()) { return d; @@ -1508,11 +1510,10 @@ public static Object stringToValue(String string) { } else { Long myLong = new Long(string); if (string.equals(myLong.toString())) { - if (myLong == myLong.intValue()) { - return myLong.intValue(); - } else { - return myLong; + if (myLong.longValue() == myLong.intValue()) { + return Integer.valueOf(myLong.intValue()); } + return myLong; } } } catch (Exception ignore) { From 39b1c0cb6682310aef7f7579836eb9561e3ad275 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 10:45:23 -0500 Subject: [PATCH 2/4] fixes error in -0 check --- JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 7b71f37b3..dc47cd2a2 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -1502,7 +1502,7 @@ public static Object stringToValue(String string) { try { if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1 - || "0".equals(string.substring(1))) { + || "-0".equals(string)) { d = Double.valueOf(string); if (!d.isInfinite() && !d.isNaN()) { return d; From 3007fc8ebe591b93caa9f6ab6b46e8677058a7a8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 15:03:19 -0500 Subject: [PATCH 3/4] Removes custom XML stringToValue method in favor of keeping a consistent implementation in JSONObject --- JSONML.java | 4 ++-- JSONObject.java | 7 +++---- XML.java | 48 ++---------------------------------------------- 3 files changed, 7 insertions(+), 52 deletions(-) diff --git a/JSONML.java b/JSONML.java index a4b874dd5..8d5e6c695 100644 --- a/JSONML.java +++ b/JSONML.java @@ -174,7 +174,7 @@ private static Object parse( if (!(token instanceof String)) { throw x.syntaxError("Missing value"); } - newjo.accumulate(attribute, XML.stringToValue((String)token)); + newjo.accumulate(attribute, JSONObject.stringToValue((String)token)); token = null; } else { newjo.accumulate(attribute, ""); @@ -227,7 +227,7 @@ private static Object parse( } else { if (ja != null) { ja.put(token instanceof String - ? XML.stringToValue((String)token) + ? JSONObject.stringToValue((String)token) : token); } } diff --git a/JSONObject.java b/JSONObject.java index dc47cd2a2..c37de6bea 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -1478,7 +1478,6 @@ public boolean similar(Object other) { * @return A simple JSON value. */ public static Object stringToValue(String string) { - Double d; if (string.equals("")) { return string; } @@ -1497,13 +1496,13 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - char b = string.charAt(0); - if ((b >= '0' && b <= '9') || b == '-') { + char initial = string.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { try { if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1 || "-0".equals(string)) { - d = Double.valueOf(string); + Double d = Double.valueOf(string); if (!d.isInfinite() && !d.isNaN()) { return d; } diff --git a/XML.java b/XML.java index f14463c94..c0e84f06b 100644 --- a/XML.java +++ b/XML.java @@ -238,7 +238,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) throw x.syntaxError("Missing value"); } jsonobject.accumulate(string, - XML.stringToValue((String) token)); + JSONObject.stringToValue((String) token)); token = null; } else { jsonobject.accumulate(string, ""); @@ -270,7 +270,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) string = (String) token; if (string.length() > 0) { jsonobject.accumulate("content", - XML.stringToValue(string)); + JSONObject.stringToValue(string)); } } else if (token == LT) { @@ -296,50 +296,6 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) } } - /** - * Try to convert a string into a number, boolean, or null. If the string - * can't be converted, return the string. This is much less ambitious than - * JSONObject.stringToValue, especially because it does not attempt to - * convert plus forms, octal forms, hex forms, or E forms lacking decimal - * points. - * - * @param string - * A String. - * @return A simple JSON value. - */ - public static Object stringToValue(String string) { - if ("true".equalsIgnoreCase(string)) { - return Boolean.TRUE; - } - if ("false".equalsIgnoreCase(string)) { - return Boolean.FALSE; - } - if ("null".equalsIgnoreCase(string)) { - return JSONObject.NULL; - } - - // If it might be a number, try converting it, first as a Long, and then - // as a Double. If that doesn't work, return the string. - try { - char initial = string.charAt(0); - if (initial == '-' || (initial >= '0' && initial <= '9')) { - Long value = new Long(string); - if (value.toString().equals(string)) { - return value; - } - } - } catch (Exception ignore) { - try { - Double value = new Double(string); - if (value.toString().equals(string)) { - return value; - } - } catch (Exception ignoreAlso) { - } - } - return string; - } - /** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. Some information may be lost in this transformation because From c2b3f2bdb164dc884efc5903ce807f7698f5dd94 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 15:21:11 -0500 Subject: [PATCH 4/4] adds back in the XML.stringToValue method, but deprecates it. --- XML.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/XML.java b/XML.java index c0e84f06b..a45ef156a 100644 --- a/XML.java +++ b/XML.java @@ -295,6 +295,18 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) } } } + + /** + * This method has been deprecated in favor of the + * {@link JSONObject.stringToValue(String)} method. Use it instead. + * + * @deprecated Use {@link JSONObject#stringToValue(String)} instead. + * @param string + * @return JSON value of this string or the string + */ + public static Object stringToValue(String string) { + return JSONObject.stringToValue(string); + } /** * Convert a well-formed (but not necessarily valid) XML string into a