1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #188 from johnjaylward/FixNegativeZero

Fix negative zero
This commit is contained in:
Sean Leary 2016-01-30 15:42:34 -06:00
commit 97f1b2744f
3 changed files with 20 additions and 52 deletions

View file

@ -174,7 +174,7 @@ public class JSONML {
if (!(token instanceof String)) { if (!(token instanceof String)) {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
newjo.accumulate(attribute, XML.stringToValue((String)token)); newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
token = null; token = null;
} else { } else {
newjo.accumulate(attribute, ""); newjo.accumulate(attribute, "");
@ -227,7 +227,7 @@ public class JSONML {
} else { } else {
if (ja != null) { if (ja != null) {
ja.put(token instanceof String ja.put(token instanceof String
? XML.stringToValue((String)token) ? JSONObject.stringToValue((String)token)
: token); : token);
} }
} }

View file

@ -30,7 +30,8 @@ import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.*; import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
@ -1477,7 +1478,6 @@ public class JSONObject {
* @return A simple JSON value. * @return A simple JSON value.
*/ */
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
Double d;
if (string.equals("")) { if (string.equals("")) {
return string; return string;
} }
@ -1496,23 +1496,23 @@ public class JSONObject {
* produced, then the value will just be a string. * produced, then the value will just be a string.
*/ */
char b = string.charAt(0); char initial = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
if (string.indexOf('.') > -1 || string.indexOf('e') > -1 if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1) { || string.indexOf('E') > -1
d = Double.valueOf(string); || "-0".equals(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) { if (!d.isInfinite() && !d.isNaN()) {
return d; return d;
} }
} else { } else {
Long myLong = new Long(string); Long myLong = new Long(string);
if (string.equals(myLong.toString())) { if (string.equals(myLong.toString())) {
if (myLong == myLong.intValue()) { if (myLong.longValue() == myLong.intValue()) {
return myLong.intValue(); return Integer.valueOf(myLong.intValue());
} else {
return myLong;
} }
return myLong;
} }
} }
} catch (Exception ignore) { } catch (Exception ignore) {

View file

@ -238,7 +238,7 @@ public class XML {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
jsonobject.accumulate(string, jsonobject.accumulate(string,
XML.stringToValue((String) token)); JSONObject.stringToValue((String) token));
token = null; token = null;
} else { } else {
jsonobject.accumulate(string, ""); jsonobject.accumulate(string, "");
@ -270,7 +270,7 @@ public class XML {
string = (String) token; string = (String) token;
if (string.length() > 0) { if (string.length() > 0) {
jsonobject.accumulate("content", jsonobject.accumulate("content",
XML.stringToValue(string)); JSONObject.stringToValue(string));
} }
} else if (token == LT) { } else if (token == LT) {
@ -295,49 +295,17 @@ public class XML {
} }
} }
} }
/** /**
* Try to convert a string into a number, boolean, or null. If the string * This method has been deprecated in favor of the
* can't be converted, return the string. This is much less ambitious than * {@link JSONObject.stringToValue(String)} method. Use it instead.
* JSONObject.stringToValue, especially because it does not attempt to
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
* points.
* *
* @deprecated Use {@link JSONObject#stringToValue(String)} instead.
* @param string * @param string
* A String. * @return JSON value of this string or the string
* @return A simple JSON value.
*/ */
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
if ("true".equalsIgnoreCase(string)) { return JSONObject.stringToValue(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;
} }
/** /**