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

non-number numbers

This commit is contained in:
Douglas Crockford 2011-10-16 10:24:08 -07:00
parent 3e3951f125
commit 88f27f3168
2 changed files with 58 additions and 46 deletions

View file

@ -86,7 +86,7 @@ import java.util.ResourceBundle;
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li> * <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2011-04-05 * @version 2011-10-16
*/ */
public class JSONObject { public class JSONObject {
@ -1226,6 +1226,7 @@ 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;
} }
@ -1260,7 +1261,10 @@ public class JSONObject {
try { try {
if (string.indexOf('.') > -1 || if (string.indexOf('.') > -1 ||
string.indexOf('e') > -1 || string.indexOf('E') > -1) { string.indexOf('e') > -1 || string.indexOf('E') > -1) {
return Double.valueOf(string); d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else { } else {
Long myLong = new Long(string); Long myLong = new Long(string);
if (myLong.longValue() == myLong.intValue()) { if (myLong.longValue() == myLong.intValue()) {

View file

@ -40,7 +40,7 @@ SOFTWARE.
* comparisons of .toString to a string literal are likely to fail. * comparisons of .toString to a string literal are likely to fail.
* *
* @author JSON.org * @author JSON.org
* @version 2011-10-05 * @version 2011-10-16
*/ */
public class Test extends TestCase { public class Test extends TestCase {
public Test(String name) { public Test(String name) {
@ -67,6 +67,10 @@ public class Test extends TestCase {
jsonobject = XML.toJSONObject(string); jsonobject = XML.toJSONObject(string);
assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}", jsonobject.toString(2)); assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}", jsonobject.toString(2));
assertEquals("<test><blank/><empty/></test>", XML.toString(jsonobject)); assertEquals("<test><blank/><empty/></test>", XML.toString(jsonobject));
string = "<subsonic-response><playlists><playlist id=\"476c65652e6d3375\"/><playlist id=\"50617274792e78737066\"/></playlists></subsonic-response>";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"subsonic-response\":\"playlists\":{\"playlist\":[{\"id\":\"476c65652e6d337\"},\"id\":\"50617274792e78737066\"}]}}}", jsonobject.toString());
} }
public void testNull() throws Exception { public void testNull() throws Exception {
@ -95,6 +99,10 @@ public class Test extends TestCase {
jsonarray = new JSONArray(string); jsonarray = new JSONArray(string);
assertEquals("[1122334455]", jsonarray.toString()); assertEquals("[1122334455]", jsonarray.toString());
string = "[666e666]";
jsonarray = new JSONArray(string);
assertEquals("[\"666e666\"]", jsonarray.toString());
string = "[00.10]"; string = "[00.10]";
jsonarray = new JSONArray(string); jsonarray = new JSONArray(string);
assertEquals("[0.1]", jsonarray.toString()); assertEquals("[0.1]", jsonarray.toString());