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 #30 from stleary/confirmValueToStringFix

Fix tabs, add valueToString() test to JSONObjectTest
This commit is contained in:
Sean Leary 2015-10-31 04:00:52 -05:00
commit c578216844
2 changed files with 202 additions and 183 deletions

View file

@ -93,29 +93,29 @@ public class JSONArrayTest {
*/ */
@Test @Test
public void verifyConstructor() { public void verifyConstructor() {
final JSONArray expected = new JSONArray("[10]"); final JSONArray expected = new JSONArray("[10]");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Collection myRawC = Collections.singleton(Integer.valueOf(10)); Collection myRawC = Collections.singleton(Integer.valueOf(10));
JSONArray jaRaw = new JSONArray(myRawC); JSONArray jaRaw = new JSONArray(myRawC);
Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10)); Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10));
JSONArray jaInt = new JSONArray(myCInt); JSONArray jaInt = new JSONArray(myCInt);
Collection<Object> myCObj = Collections.singleton((Object) Integer Collection<Object> myCObj = Collections.singleton((Object) Integer
.valueOf(10)); .valueOf(10));
JSONArray jaObj = new JSONArray(myCObj); JSONArray jaObj = new JSONArray(myCObj);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaInt)); expected.similar(jaInt));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObj)); expected.similar(jaObj));
} }
/** /**
@ -123,32 +123,32 @@ public class JSONArrayTest {
*/ */
@Test @Test
public void verifyPutCollection() { public void verifyPutCollection() {
final JSONArray expected = new JSONArray("[[10]]"); final JSONArray expected = new JSONArray("[[10]]");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Collection myRawC = Collections.singleton(Integer.valueOf(10)); Collection myRawC = Collections.singleton(Integer.valueOf(10));
JSONArray jaRaw = new JSONArray(); JSONArray jaRaw = new JSONArray();
jaRaw.put(myRawC); jaRaw.put(myRawC);
Collection<Object> myCObj = Collections.singleton((Object) Integer Collection<Object> myCObj = Collections.singleton((Object) Integer
.valueOf(10)); .valueOf(10));
JSONArray jaObj = new JSONArray(); JSONArray jaObj = new JSONArray();
jaObj.put(myCObj); jaObj.put(myCObj);
Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10)); Collection<Integer> myCInt = Collections.singleton(Integer.valueOf(10));
JSONArray jaInt = new JSONArray(); JSONArray jaInt = new JSONArray();
jaInt.put(myCInt); jaInt.put(myCInt);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObj)); expected.similar(jaObj));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaInt)); expected.similar(jaInt));
} }
@ -157,41 +157,41 @@ public class JSONArrayTest {
*/ */
@Test @Test
public void verifyPutMap() { public void verifyPutMap() {
final JSONArray expected = new JSONArray("[{\"myKey\":10}]"); final JSONArray expected = new JSONArray("[{\"myKey\":10}]");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
JSONArray jaRaw = new JSONArray(); JSONArray jaRaw = new JSONArray();
jaRaw.put(myRawC); jaRaw.put(myRawC);
Map<String, Object> myCStrObj = Collections.singletonMap("myKey", Map<String, Object> myCStrObj = Collections.singletonMap("myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONArray jaStrObj = new JSONArray(); JSONArray jaStrObj = new JSONArray();
jaStrObj.put(myCStrObj); jaStrObj.put(myCStrObj);
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey", Map<String, Integer> myCStrInt = Collections.singletonMap("myKey",
Integer.valueOf(10)); Integer.valueOf(10));
JSONArray jaStrInt = new JSONArray(); JSONArray jaStrInt = new JSONArray();
jaStrInt.put(myCStrInt); jaStrInt.put(myCStrInt);
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey", Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONArray jaObjObj = new JSONArray(); JSONArray jaObjObj = new JSONArray();
jaObjObj.put(myCObjObj); jaObjObj.put(myCObjObj);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrObj)); expected.similar(jaStrObj));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrInt)); expected.similar(jaStrInt));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObjObj)); expected.similar(jaObjObj));
} }
/** /**

View file

@ -172,37 +172,37 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void verifyConstructor() { public void verifyConstructor() {
final JSONObject expected = new JSONObject("{\"myKey\":10}"); final JSONObject expected = new JSONObject("{\"myKey\":10}");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
JSONObject jaRaw = new JSONObject(myRawC); JSONObject jaRaw = new JSONObject(myRawC);
Map<String, Object> myCStrObj = Collections.singletonMap("myKey", Map<String, Object> myCStrObj = Collections.singletonMap("myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONObject jaStrObj = new JSONObject(myCStrObj); JSONObject jaStrObj = new JSONObject(myCStrObj);
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey", Map<String, Integer> myCStrInt = Collections.singletonMap("myKey",
Integer.valueOf(10)); Integer.valueOf(10));
JSONObject jaStrInt = new JSONObject(myCStrInt); JSONObject jaStrInt = new JSONObject(myCStrInt);
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey", Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONObject jaObjObj = new JSONObject(myCObjObj); JSONObject jaObjObj = new JSONObject(myCObjObj);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrObj)); expected.similar(jaStrObj));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrInt)); expected.similar(jaStrInt));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObjObj)); expected.similar(jaObjObj));
} }
/** /**
@ -210,33 +210,33 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void verifyPutCollection() { public void verifyPutCollection() {
final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); final JSONObject expected = new JSONObject("{\"myCollection\":[10]}");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Collection myRawC = Collections.singleton(Integer.valueOf(10)); Collection myRawC = Collections.singleton(Integer.valueOf(10));
JSONObject jaRaw = new JSONObject(); JSONObject jaRaw = new JSONObject();
jaRaw.put("myCollection", myRawC); jaRaw.put("myCollection", myRawC);
Collection<Object> myCObj = Collections.singleton((Object) Integer Collection<Object> myCObj = Collections.singleton((Object) Integer
.valueOf(10)); .valueOf(10));
JSONObject jaObj = new JSONObject(); JSONObject jaObj = new JSONObject();
jaObj.put("myCollection", myCObj); jaObj.put("myCollection", myCObj);
Collection<Integer> myCInt = Collections.singleton(Integer Collection<Integer> myCInt = Collections.singleton(Integer
.valueOf(10)); .valueOf(10));
JSONObject jaInt = new JSONObject(); JSONObject jaInt = new JSONObject();
jaInt.put("myCollection", myCInt); jaInt.put("myCollection", myCInt);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObj)); expected.similar(jaObj));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaInt)); expected.similar(jaInt));
} }
@ -245,41 +245,41 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void verifyPutMap() { public void verifyPutMap() {
final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}");
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
JSONObject jaRaw = new JSONObject(); JSONObject jaRaw = new JSONObject();
jaRaw.put("myMap", myRawC); jaRaw.put("myMap", myRawC);
Map<String, Object> myCStrObj = Collections.singletonMap("myKey", Map<String, Object> myCStrObj = Collections.singletonMap("myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONObject jaStrObj = new JSONObject(); JSONObject jaStrObj = new JSONObject();
jaStrObj.put("myMap", myCStrObj); jaStrObj.put("myMap", myCStrObj);
Map<String, Integer> myCStrInt = Collections.singletonMap("myKey", Map<String, Integer> myCStrInt = Collections.singletonMap("myKey",
Integer.valueOf(10)); Integer.valueOf(10));
JSONObject jaStrInt = new JSONObject(); JSONObject jaStrInt = new JSONObject();
jaStrInt.put("myMap", myCStrInt); jaStrInt.put("myMap", myCStrInt);
Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey", Map<?, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
(Object) Integer.valueOf(10)); (Object) Integer.valueOf(10));
JSONObject jaObjObj = new JSONObject(); JSONObject jaObjObj = new JSONObject();
jaObjObj.put("myMap", myCObjObj); jaObjObj.put("myMap", myCObjObj);
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaRaw)); expected.similar(jaRaw));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrObj)); expected.similar(jaStrObj));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaStrInt)); expected.similar(jaStrInt));
assertTrue( assertTrue(
"The RAW Collection should give me the same as the Typed Collection", "The RAW Collection should give me the same as the Typed Collection",
expected.similar(jaObjObj)); expected.similar(jaObjObj));
} }
@ -640,7 +640,7 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void jsonInvalidNumberValues() { public void jsonInvalidNumberValues() {
// Number-notations supported by Java and invalid as JSON // Number-notations supported by Java and invalid as JSON
String str = String str =
"{"+ "{"+
"\"hexNumber\":-0x123,"+ "\"hexNumber\":-0x123,"+
@ -1180,7 +1180,7 @@ public class JSONObjectTest {
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
/** /**
* float f = 3.1f; * float f = 3.1f;
* double df = (double) f; * double df = (double) f;
* double d = 3.1d; * double d = 3.1d;
@ -1220,11 +1220,11 @@ public class JSONObjectTest {
assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float );
inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double!
// this.put(key, (Float) value + 1); // this.put(key, (Float) value + 1);
// 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done.
// 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects!
// 3. A float+float operation will be performed and results into a float primitive. // 3. A float+float operation will be performed and results into a float primitive.
// 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method
// put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa
assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double );
// correct implementation (with change of behavior) would be: // correct implementation (with change of behavior) would be:
// this.put(key, new Float((Float) value + 1)); // this.put(key, new Float((Float) value + 1));
@ -1397,30 +1397,30 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void jsonObjectToStringSuppressWarningOnCastToCollection() { public void jsonObjectToStringSuppressWarningOnCastToCollection() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
Collection<String> collection = new ArrayList<String>(); Collection<String> collection = new ArrayList<String>();
collection.add("abc"); collection.add("abc");
// ArrayList will be added as an object // ArrayList will be added as an object
jsonObject.put("key", collection); jsonObject.put("key", collection);
String toStr = jsonObject.toString(); String toStr = jsonObject.toString();
// [abc] will be added as a JSONArray // [abc] will be added as a JSONArray
JSONObject expectedJsonObject = new JSONObject(toStr); JSONObject expectedJsonObject = new JSONObject(toStr);
/** /**
* Can't do a Util compare because although they look the same in the * Can't do a Util compare because although they look the same in the
* debugger, one is a collection and the other is a JSONArray. * debugger, one is a collection and the other is a JSONArray.
*/ */
assertTrue("keys should be equal", jsonObject.keySet().iterator() assertTrue("keys should be equal", jsonObject.keySet().iterator()
.next().equals(expectedJsonObject.keySet().iterator().next())); .next().equals(expectedJsonObject.keySet().iterator().next()));
assertTrue("Collections should be converted to JSONArray", assertTrue("Collections should be converted to JSONArray",
jsonObject.get("key") instanceof JSONArray); jsonObject.get("key") instanceof JSONArray);
JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); JSONArray jsonArray = expectedJsonObject.getJSONArray("key");
assertTrue("value size should be equal", assertTrue("value size should be equal",
collection.size() == jsonArray.length()); collection.size() == jsonArray.length());
Iterator<String> it = collection.iterator(); Iterator<String> it = collection.iterator();
for (int i = 0; i < collection.size(); ++i) { for (int i = 0; i < collection.size(); ++i) {
assertTrue("items should be equal for index: " + i, jsonArray assertTrue("items should be equal for index: " + i, jsonArray
.get(i).toString().equals(it.next().toString())); .get(i).toString().equals(it.next().toString()));
} }
} }
/** /**
@ -1471,6 +1471,25 @@ public class JSONObjectTest {
jsonArray.toString().equals(JSONObject.valueToString(array))); jsonArray.toString().equals(JSONObject.valueToString(array)));
} }
/**
* Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed.
* The following code was throwing a ClassCastException in the
* JSONObject(Map<String, Object>) constructor
*/
@Test
public void valueToStringConfirmException() {
String expectedStr = "{\"1\":\"myValue\"}";
Map<Integer, String> myMap = new HashMap<Integer, String>();
myMap.put(1, "myValue");
// this is the test, it should not throw an exception
String str = JSONObject.valueToString(myMap);
// confirm result, just in case
JSONObject jsonObject = new JSONObject(str);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,
expectedJsonObject);
}
/** /**
* Exercise the JSONObject wrap() method. Sometimes wrap() will change * Exercise the JSONObject wrap() method. Sometimes wrap() will change
* the object being wrapped, other times not. The purpose of wrap() is * the object being wrapped, other times not. The purpose of wrap() is