Skip to content

Commit 97f1b27

Browse files
committed
Merge pull request #188 from johnjaylward/FixNegativeZero
Fix negative zero
2 parents 9950350 + c2b3f2b commit 97f1b27

File tree

3 files changed

+20
-52
lines changed

3 files changed

+20
-52
lines changed

JSONML.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static Object parse(
174174
if (!(token instanceof String)) {
175175
throw x.syntaxError("Missing value");
176176
}
177-
newjo.accumulate(attribute, XML.stringToValue((String)token));
177+
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
178178
token = null;
179179
} else {
180180
newjo.accumulate(attribute, "");
@@ -227,7 +227,7 @@ private static Object parse(
227227
} else {
228228
if (ja != null) {
229229
ja.put(token instanceof String
230-
? XML.stringToValue((String)token)
230+
? JSONObject.stringToValue((String)token)
231231
: token);
232232
}
233233
}

JSONObject.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Field;
3131
import java.lang.reflect.Method;
3232
import java.lang.reflect.Modifier;
33-
import java.math.*;
33+
import java.math.BigDecimal;
34+
import java.math.BigInteger;
3435
import java.util.Collection;
3536
import java.util.Enumeration;
3637
import java.util.HashMap;
@@ -1477,7 +1478,6 @@ public boolean similar(Object other) {
14771478
* @return A simple JSON value.
14781479
*/
14791480
public static Object stringToValue(String string) {
1480-
Double d;
14811481
if (string.equals("")) {
14821482
return string;
14831483
}
@@ -1496,23 +1496,23 @@ public static Object stringToValue(String string) {
14961496
* produced, then the value will just be a string.
14971497
*/
14981498

1499-
char b = string.charAt(0);
1500-
if ((b >= '0' && b <= '9') || b == '-') {
1499+
char initial = string.charAt(0);
1500+
if ((initial >= '0' && initial <= '9') || initial == '-') {
15011501
try {
15021502
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
1503-
|| string.indexOf('E') > -1) {
1504-
d = Double.valueOf(string);
1503+
|| string.indexOf('E') > -1
1504+
|| "-0".equals(string)) {
1505+
Double d = Double.valueOf(string);
15051506
if (!d.isInfinite() && !d.isNaN()) {
15061507
return d;
15071508
}
15081509
} else {
15091510
Long myLong = new Long(string);
15101511
if (string.equals(myLong.toString())) {
1511-
if (myLong == myLong.intValue()) {
1512-
return myLong.intValue();
1513-
} else {
1514-
return myLong;
1512+
if (myLong.longValue() == myLong.intValue()) {
1513+
return Integer.valueOf(myLong.intValue());
15151514
}
1515+
return myLong;
15161516
}
15171517
}
15181518
} catch (Exception ignore) {

XML.java

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
238238
throw x.syntaxError("Missing value");
239239
}
240240
jsonobject.accumulate(string,
241-
XML.stringToValue((String) token));
241+
JSONObject.stringToValue((String) token));
242242
token = null;
243243
} else {
244244
jsonobject.accumulate(string, "");
@@ -270,7 +270,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
270270
string = (String) token;
271271
if (string.length() > 0) {
272272
jsonobject.accumulate("content",
273-
XML.stringToValue(string));
273+
JSONObject.stringToValue(string));
274274
}
275275

276276
} else if (token == LT) {
@@ -295,49 +295,17 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
295295
}
296296
}
297297
}
298-
298+
299299
/**
300-
* Try to convert a string into a number, boolean, or null. If the string
301-
* can't be converted, return the string. This is much less ambitious than
302-
* JSONObject.stringToValue, especially because it does not attempt to
303-
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
304-
* points.
300+
* This method has been deprecated in favor of the
301+
* {@link JSONObject.stringToValue(String)} method. Use it instead.
305302
*
303+
* @deprecated Use {@link JSONObject#stringToValue(String)} instead.
306304
* @param string
307-
* A String.
308-
* @return A simple JSON value.
305+
* @return JSON value of this string or the string
309306
*/
310307
public static Object stringToValue(String string) {
311-
if ("true".equalsIgnoreCase(string)) {
312-
return Boolean.TRUE;
313-
}
314-
if ("false".equalsIgnoreCase(string)) {
315-
return Boolean.FALSE;
316-
}
317-
if ("null".equalsIgnoreCase(string)) {
318-
return JSONObject.NULL;
319-
}
320-
321-
// If it might be a number, try converting it, first as a Long, and then
322-
// as a Double. If that doesn't work, return the string.
323-
try {
324-
char initial = string.charAt(0);
325-
if (initial == '-' || (initial >= '0' && initial <= '9')) {
326-
Long value = new Long(string);
327-
if (value.toString().equals(string)) {
328-
return value;
329-
}
330-
}
331-
} catch (Exception ignore) {
332-
try {
333-
Double value = new Double(string);
334-
if (value.toString().equals(string)) {
335-
return value;
336-
}
337-
} catch (Exception ignoreAlso) {
338-
}
339-
}
340-
return string;
308+
return JSONObject.stringToValue(string);
341309
}
342310

343311
/**

0 commit comments

Comments
 (0)