diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 0a730f4..7e8cbbe 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2392,8 +2392,14 @@ public class JSONObject { */ protected static Number stringToNumber(final String input) throws NumberFormatException { String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if ((initial >= '0' && initial <= '9') || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -2424,13 +2430,13 @@ public class JSONObject { if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } // integer representation. @@ -2450,7 +2456,7 @@ public class JSONObject { } return bi; } - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } /** @@ -2486,8 +2492,7 @@ public class JSONObject { * produced, then the value will just be a string. */ - char initial = string.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if (potentialNumber(string)) { try { return stringToNumber(string); } catch (Exception ignore) { @@ -2496,6 +2501,28 @@ public class JSONObject { return string; } + + private static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + /** * Throw an exception if the object is a NaN or infinite number. * @@ -2902,26 +2929,15 @@ public class JSONObject { * @return number without leading zeros */ private static String removeLeadingZerosOfNumber(String value){ - char[] chars = value.toCharArray(); - int leftMostUnsignedIndex = 0; - if (chars[0] == '-'){ - leftMostUnsignedIndex = 1; - } - int firstNonZeroCharIndex = -1; - for (int i=leftMostUnsignedIndex;i