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

Merge pull request #71 from johnjaylward/OptimizeOpt

Adjustments to tests for https://github.com/stleary/JSON-java/pull/337
This commit is contained in:
Sean Leary 2017-05-22 22:59:37 -05:00 committed by GitHub
commit 93ca7b176f
2 changed files with 152 additions and 38 deletions

View file

@ -393,6 +393,20 @@ public class JSONArrayTest {
assertTrue("Array opt double default implicit", assertTrue("Array opt double default implicit",
new Double(jsonArray.optDouble(99)).isNaN()); new Double(jsonArray.optDouble(99)).isNaN());
assertTrue("Array opt float",
new Float(23.45e-4).equals(jsonArray.optFloat(5)));
assertTrue("Array opt float default",
new Float(1).equals(jsonArray.optFloat(0, 1)));
assertTrue("Array opt float default implicit",
new Float(jsonArray.optFloat(99)).isNaN());
assertTrue("Array opt Number",
new Double(23.45e-4).equals(jsonArray.optNumber(5)));
assertTrue("Array opt Number default",
new Double(1).equals(jsonArray.optNumber(0, 1d)));
assertTrue("Array opt Number default implicit",
new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN());
assertTrue("Array opt int", assertTrue("Array opt int",
new Integer(42).equals(jsonArray.optInt(7))); new Integer(42).equals(jsonArray.optInt(7)));
assertTrue("Array opt int default", assertTrue("Array opt int default",

View file

@ -2,8 +2,10 @@ package org.json.junit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -513,7 +515,7 @@ public class JSONObjectTest {
// include an unsupported object for coverage // include an unsupported object for coverage
try { try {
jsonObject.accumulate("myArray", Double.NaN); jsonObject.accumulate("myArray", Double.NaN);
assertTrue("Expected exception", false); fail("Expected exception");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
// validate JSON // validate JSON
@ -544,7 +546,7 @@ public class JSONObjectTest {
// include an unsupported object for coverage // include an unsupported object for coverage
try { try {
jsonObject.append("myArray", Double.NaN); jsonObject.append("myArray", Double.NaN);
assertTrue("Expected exception", false); fail("Expected exception");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
// validate JSON // validate JSON
@ -594,6 +596,9 @@ public class JSONObjectTest {
"\"longStrKey\":\"987654321098765432\","+ "\"longStrKey\":\"987654321098765432\","+
"\"doubleKey\":-23.45e7,"+ "\"doubleKey\":-23.45e7,"+
"\"doubleStrKey\":\"00001.000\","+ "\"doubleStrKey\":\"00001.000\","+
"\"BigDecimalStrKey\":\"19007199254740993.35481234487103587486413587843213584\","+
"\"negZeroKey\":-0.0,"+
"\"negZeroStrKey\":\"-0.0\","+
"\"arrayKey\":[0,1,2],"+ "\"arrayKey\":[0,1,2],"+
"\"objectKey\":{\"myKey\":\"myVal\"}"+ "\"objectKey\":{\"myKey\":\"myVal\"}"+
"}"; "}";
@ -610,10 +615,30 @@ public class JSONObjectTest {
jsonObject.getDouble("doubleKey") == -23.45e7); jsonObject.getDouble("doubleKey") == -23.45e7);
assertTrue("doubleStrKey should be double", assertTrue("doubleStrKey should be double",
jsonObject.getDouble("doubleStrKey") == 1); jsonObject.getDouble("doubleStrKey") == 1);
assertTrue("doubleKey can be float",
jsonObject.getFloat("doubleKey") == -23.45e7f);
assertTrue("doubleStrKey can be float",
jsonObject.getFloat("doubleStrKey") == 1f);
assertTrue("opt doubleKey should be double", assertTrue("opt doubleKey should be double",
jsonObject.optDouble("doubleKey") == -23.45e7); jsonObject.optDouble("doubleKey") == -23.45e7);
assertTrue("opt doubleKey with Default should be double", assertTrue("opt doubleKey with Default should be double",
jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
assertTrue("opt negZeroKey should be double",
Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);
assertTrue("opt negZeroStrKey with Default should be double",
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
assertTrue("optNumber negZeroKey should return Double",
jsonObject.optNumber("negZeroKey") instanceof Double);
assertTrue("optNumber negZeroStrKey should return Double",
jsonObject.optNumber("negZeroStrKey") instanceof Double);
assertTrue("optNumber negZeroKey should be -0.0",
Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
assertTrue("optNumber negZeroStrKey should be -0.0",
Double.compare(jsonObject.optNumber("negZeroStrKey").doubleValue(), -0.0d) == 0);
assertTrue("optFloat doubleKey should be float",
jsonObject.optFloat("doubleKey") == -23.45e7f);
assertTrue("optFloat doubleKey with Default should be float",
jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
assertTrue("intKey should be int", assertTrue("intKey should be int",
jsonObject.optInt("intKey") == 42); jsonObject.optInt("intKey") == 42);
assertTrue("opt intKey should be int", assertTrue("opt intKey should be int",
@ -630,6 +655,20 @@ public class JSONObjectTest {
jsonObject.optLong("longKey", 0) == 1234567890123456789L); jsonObject.optLong("longKey", 0) == 1234567890123456789L);
assertTrue("longStrKey should be long", assertTrue("longStrKey should be long",
jsonObject.getLong("longStrKey") == 987654321098765432L); jsonObject.getLong("longStrKey") == 987654321098765432L);
assertTrue("optNumber int should return Integer",
jsonObject.optNumber("intKey") instanceof Integer);
assertTrue("optNumber long should return Long",
jsonObject.optNumber("longKey") instanceof Long);
assertTrue("optNumber double should return Double",
jsonObject.optNumber("doubleKey") instanceof Double);
assertTrue("optNumber Str int should return Integer",
jsonObject.optNumber("intStrKey") instanceof Integer);
assertTrue("optNumber Str long should return Long",
jsonObject.optNumber("longStrKey") instanceof Long);
assertTrue("optNumber Str double should return Double",
jsonObject.optNumber("doubleStrKey") instanceof Double);
assertTrue("optNumber BigDecimalStrKey should return BigDecimal",
jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
assertTrue("xKey should not exist", assertTrue("xKey should not exist",
jsonObject.isNull("xKey")); jsonObject.isNull("xKey"));
assertTrue("stringKey should exist", assertTrue("stringKey should exist",
@ -669,7 +708,7 @@ public class JSONObjectTest {
* This test documents a need for BigDecimal conversion. * This test documents a need for BigDecimal conversion.
*/ */
Object obj = JSONObject.stringToValue( "299792.457999999984" ); Object obj = JSONObject.stringToValue( "299792.457999999984" );
assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", assertTrue( "evaluates to 299792.458 double instead of 299792.457999999984 BigDecimal!",
obj.equals(new Double(299792.458)) ); obj.equals(new Double(299792.458)) );
assertTrue( "1 should be an Integer!", assertTrue( "1 should be an Integer!",
JSONObject.stringToValue( "1" ) instanceof Integer ); JSONObject.stringToValue( "1" ) instanceof Integer );
@ -787,14 +826,14 @@ public class JSONObjectTest {
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
try { try {
jsonObject.getBoolean("nonKey"); jsonObject.getBoolean("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("expecting an exception message", assertTrue("expecting an exception message",
"JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); "JSONObject[\"nonKey\"] not found.".equals(e.getMessage()));
} }
try { try {
jsonObject.getBoolean("stringKey"); jsonObject.getBoolean("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a Boolean.". "JSONObject[\"stringKey\"] is not a Boolean.".
@ -802,7 +841,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getString("nonKey"); jsonObject.getString("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -810,7 +849,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getString("trueKey"); jsonObject.getString("trueKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"trueKey\"] not a string.". "JSONObject[\"trueKey\"] not a string.".
@ -818,7 +857,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getDouble("nonKey"); jsonObject.getDouble("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -826,7 +865,23 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getDouble("stringKey"); jsonObject.getDouble("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a number.".
equals(e.getMessage()));
}
try {
jsonObject.getFloat("nonKey");
fail("Expected an exception");
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.".
equals(e.getMessage()));
}
try {
jsonObject.getFloat("stringKey");
fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a number.". "JSONObject[\"stringKey\"] is not a number.".
@ -834,7 +889,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getInt("nonKey"); jsonObject.getInt("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -842,7 +897,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getInt("stringKey"); jsonObject.getInt("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not an int.". "JSONObject[\"stringKey\"] is not an int.".
@ -850,7 +905,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getLong("nonKey"); jsonObject.getLong("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -858,7 +913,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getLong("stringKey"); jsonObject.getLong("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a long.". "JSONObject[\"stringKey\"] is not a long.".
@ -866,7 +921,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getJSONArray("nonKey"); jsonObject.getJSONArray("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -874,7 +929,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getJSONArray("stringKey"); jsonObject.getJSONArray("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a JSONArray.". "JSONObject[\"stringKey\"] is not a JSONArray.".
@ -882,7 +937,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getJSONObject("nonKey"); jsonObject.getJSONObject("nonKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"nonKey\"] not found.". "JSONObject[\"nonKey\"] not found.".
@ -890,7 +945,7 @@ public class JSONObjectTest {
} }
try { try {
jsonObject.getJSONObject("stringKey"); jsonObject.getJSONObject("stringKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[\"stringKey\"] is not a JSONObject.". "JSONObject[\"stringKey\"] is not a JSONObject.".
@ -987,21 +1042,22 @@ public class JSONObjectTest {
*/ */
try { try {
jsonObject.getBigDecimal("bigInt"); jsonObject.getBigDecimal("bigInt");
assertTrue("expected an exeption", false); fail("expected an exeption");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
obj = jsonObject.optBigDecimal("bigInt", BigDecimal.ONE); obj = jsonObject.optBigDecimal("bigInt", BigDecimal.ONE);
assertTrue("expected BigDecimal", obj.equals(BigDecimal.ONE)); assertTrue("expected BigDecimal", obj.equals(BigDecimal.ONE));
try { try {
jsonObject.getBigInteger("bigDec"); jsonObject.getBigInteger("bigDec");
assertTrue("expected an exeption", false); fail("expected an exeption");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
jsonObject.put("stringKey", "abc"); jsonObject.put("stringKey", "abc");
try { try {
jsonObject.getBigDecimal("stringKey"); jsonObject.getBigDecimal("stringKey");
assertTrue("expected an exeption", false); fail("expected an exeption");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE); obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE);
assertTrue("expected BigInteger", obj.equals(BigInteger.ONE)); assertTrue("expected BigInteger", obj instanceof BigInteger);
assertEquals(bigDecimal.toBigInteger(), obj);
/** /**
* JSONObject.numberToString() works correctly, nothing to change. * JSONObject.numberToString() works correctly, nothing to change.
@ -1074,11 +1130,11 @@ public class JSONObjectTest {
jsonArray.put(Boolean.TRUE); jsonArray.put(Boolean.TRUE);
try { try {
jsonArray.getBigInteger(2); jsonArray.getBigInteger(2);
assertTrue("should not be able to get big int", false); fail("should not be able to get big int");
} catch (Exception ignored) {} } catch (Exception ignored) {}
try { try {
jsonArray.getBigDecimal(2); jsonArray.getBigDecimal(2);
assertTrue("should not be able to get big dec", false); fail("should not be able to get big dec");
} catch (Exception ignored) {} } catch (Exception ignored) {}
assertTrue("optBigInt is default", jsonArray.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE)); assertTrue("optBigInt is default", jsonArray.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE));
assertTrue("optBigDec is default", jsonArray.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE)); assertTrue("optBigDec is default", jsonArray.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE));
@ -1833,7 +1889,7 @@ public class JSONObjectTest {
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
jsonObject.append("myKey", "hello"); jsonObject.append("myKey", "hello");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"JSONObject[myKey] is not a JSONArray.". "JSONObject[myKey] is not a JSONArray.".
@ -1844,7 +1900,7 @@ public class JSONObjectTest {
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
jsonObject.increment("myKey"); jsonObject.increment("myKey");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"Unable to increment [\"myKey\"].". "Unable to increment [\"myKey\"].".
@ -1855,7 +1911,7 @@ public class JSONObjectTest {
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
jsonObject.get(null); jsonObject.get(null);
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"Null key.". "Null key.".
@ -1864,7 +1920,7 @@ public class JSONObjectTest {
try { try {
// invalid numberToString() // invalid numberToString()
JSONObject.numberToString((Number)null); JSONObject.numberToString((Number)null);
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Expecting an exception message", assertTrue("Expecting an exception message",
"Null pointer". "Null pointer".
@ -1874,7 +1930,7 @@ public class JSONObjectTest {
// null put key // null put key
JSONObject jsonObject = new JSONObject("{}"); JSONObject jsonObject = new JSONObject("{}");
jsonObject.put(null, 0); jsonObject.put(null, 0);
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (NullPointerException ignored) { } catch (NullPointerException ignored) {
} }
try { try {
@ -1882,21 +1938,21 @@ public class JSONObjectTest {
JSONObject jsonObject = new JSONObject("{}"); JSONObject jsonObject = new JSONObject("{}");
jsonObject.putOnce("hello", "world"); jsonObject.putOnce("hello", "world");
jsonObject.putOnce("hello", "world!"); jsonObject.putOnce("hello", "world!");
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
try { try {
// test validity of invalid double // test validity of invalid double
JSONObject.testValidity(Double.NaN); JSONObject.testValidity(Double.NaN);
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
try { try {
// test validity of invalid float // test validity of invalid float
JSONObject.testValidity(Float.NEGATIVE_INFINITY); JSONObject.testValidity(Float.NEGATIVE_INFINITY);
assertTrue("Expected an exception", false); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
@ -1936,9 +1992,13 @@ public class JSONObjectTest {
assertTrue("optJSONObject() should return null ", assertTrue("optJSONObject() should return null ",
null==jsonObject.optJSONObject("myKey")); null==jsonObject.optJSONObject("myKey"));
assertTrue("optLong() should return default long", assertTrue("optLong() should return default long",
42 == jsonObject.optLong("myKey", 42)); 42l == jsonObject.optLong("myKey", 42l));
assertTrue("optDouble() should return default double", assertTrue("optDouble() should return default double",
42.3 == jsonObject.optDouble("myKey", 42.3)); 42.3d == jsonObject.optDouble("myKey", 42.3d));
assertTrue("optFloat() should return default float",
42.3f == jsonObject.optFloat("myKey", 42.3f));
assertTrue("optNumber() should return default Number",
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
assertTrue("optString() should return default string", assertTrue("optString() should return default string",
"hi".equals(jsonObject.optString("hiKey", "hi"))); "hi".equals(jsonObject.optString("hiKey", "hi")));
} }
@ -1966,9 +2026,13 @@ public class JSONObjectTest {
assertTrue("optJSONObject() should return null ", assertTrue("optJSONObject() should return null ",
null==jsonObject.optJSONObject("myKey")); null==jsonObject.optJSONObject("myKey"));
assertTrue("optLong() should return default long", assertTrue("optLong() should return default long",
42 == jsonObject.optLong("myKey", 42)); 42l == jsonObject.optLong("myKey", 42l));
assertTrue("optDouble() should return default double", assertTrue("optDouble() should return default double",
42.3 == jsonObject.optDouble("myKey", 42.3)); 42.3d == jsonObject.optDouble("myKey", 42.3d));
assertTrue("optFloat() should return default float",
42.3f == jsonObject.optFloat("myKey", 42.3f));
assertTrue("optNumber() should return default Number",
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
assertTrue("optString() should return default string", assertTrue("optString() should return default string",
"hi".equals(jsonObject.optString("hiKey", "hi"))); "hi".equals(jsonObject.optString("hiKey", "hi")));
} }
@ -1982,11 +2046,47 @@ public class JSONObjectTest {
assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true);
assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false);
assertTrue("unexpected optInt value",jo.optInt("int",0)==123); assertTrue("unexpected optInt value",jo.optInt("int",0)==123);
assertTrue("unexpected optLong value",jo.optLong("int",0)==123); assertTrue("unexpected optLong value",jo.optLong("int",0)==123l);
assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d);
assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f);
assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l);
}
/**
* Verifies that the opt methods properly convert string values to numbers and coerce them consistently.
*/
@Test
public void jsonObjectOptCoercion() {
JSONObject jo = new JSONObject("{\"largeNumberStr\":\"19007199254740993.35481234487103587486413587843213584\"}");
// currently the parser doesn't recognize BigDecimal, to we have to put it manually
jo.put("largeNumber", new BigDecimal("19007199254740993.35481234487103587486413587843213584"));
// Test type coercion from larger to smaller
assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumber",null));
assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null));
assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0);
assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f);
assertEquals(19007199254740993l, jo.optLong("largeNumber"));
assertEquals(1874919425, jo.optInt("largeNumber"));
// conversion from a string
assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumberStr",null));
assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumberStr",null));
assertEquals(1.9007199254740992E16, jo.optDouble("largeNumberStr"),0.0);
assertEquals(1.90071995E16f, jo.optFloat("largeNumberStr"),0.0f);
assertEquals(19007199254740993l, jo.optLong("largeNumberStr"));
assertEquals(1874919425, jo.optInt("largeNumberStr"));
// the integer portion of the actual value is larger than a double can hold.
assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber"));
assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber"));
assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumberStr"));
assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumberStr"));
assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
} }
/** /**
@ -2253,7 +2353,7 @@ public class JSONObjectTest {
// assertTrue("should convert null to empty string", "".equals(string)); // assertTrue("should convert null to empty string", "".equals(string));
try { try {
value = jsonObjectNull.get("key"); value = jsonObjectNull.get("key");
assertTrue("get() null should throw exception", false); fail("get() null should throw exception");
} catch (Exception ignored) {} } catch (Exception ignored) {}
/** /**