Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative zero #188

Merged
merged 7 commits into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions JSONML.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down Expand Up @@ -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);
}
}
Expand Down
20 changes: 10 additions & 10 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1477,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;
}
Expand All @@ -1496,23 +1496,23 @@ 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) {
d = Double.valueOf(string);
|| string.indexOf('E') > -1
|| "-0".equals(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} 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) {
Expand Down
48 changes: 8 additions & 40 deletions XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down Expand Up @@ -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) {
Expand All @@ -295,49 +295,17 @@ 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.
* 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
* A String.
* @return A simple JSON value.
* @return JSON value of this string or the string
*/
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;
return JSONObject.stringToValue(string);
}

/**
Expand Down