From 3007fc8ebe591b93caa9f6ab6b46e8677058a7a8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 15:03:19 -0500 Subject: [PATCH] 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 a4b874d..8d5e6c6 100644 --- a/JSONML.java +++ b/JSONML.java @@ -174,7 +174,7 @@ public class JSONML { 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 @@ public class JSONML { } 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 dc47cd2..c37de6b 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -1478,7 +1478,6 @@ public class JSONObject { * @return A simple JSON value. */ public static Object stringToValue(String string) { - Double d; if (string.equals("")) { return string; } @@ -1497,13 +1496,13 @@ public class JSONObject { * 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 f14463c..c0e84f0 100644 --- a/XML.java +++ b/XML.java @@ -238,7 +238,7 @@ public class XML { 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 @@ public class XML { string = (String) token; if (string.length() > 0) { jsonobject.accumulate("content", - XML.stringToValue(string)); + JSONObject.stringToValue(string)); } } else if (token == LT) { @@ -296,50 +296,6 @@ public class XML { } } - /** - * 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