Skip to content
Closed
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
36 changes: 36 additions & 0 deletions src/test/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.json.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.*;
Expand Down Expand Up @@ -686,4 +687,39 @@ public void iterator() {
new Long(-1).equals(Long.parseLong((String) it.next())));
assertTrue("should be at end of array", !it.hasNext());
}

/**
* Exercise toList.
*/
@Test
public void toList() {
assertEquals("zero length array should return zero length list",
0, new JSONArray().toList().size());

JSONArray jsonArray = new JSONArray(arrayStr);
List<Object> jsonArrayAsList = jsonArray.toList();
assertEquals("lengths should be equals",
jsonArray.length(), jsonArrayAsList.size());
assertEquals("booleans should remain booleans",
true, jsonArrayAsList.get(0));
assertEquals("strings should remain strings",
"hello", jsonArrayAsList.get(4));
assertTrue("arrays should be converted to lists",
List.class.isAssignableFrom(jsonArrayAsList.get(9).getClass()));
assertEquals("arrays should be converted to lists with the same size",
1, ((List) jsonArrayAsList.get(9)).size());
assertEquals("arrays should be converted to lists with the same values",
"world", ((List) jsonArrayAsList.get(9)).get(0));
assertTrue("JSONObjects should be converted to maps",
Map.class.isAssignableFrom(jsonArrayAsList.get(10).getClass()));
assertEquals("JSONObjects should be converted to maps with the same size",
4, ((Map) jsonArrayAsList.get(10)).size());
assertTrue("JSONObjects should be converted to maps with the same keys",
((Map) jsonArrayAsList.get(10)).containsKey("key1"));

jsonArray = new JSONArray("[null]");
jsonArrayAsList = jsonArray.toList();
assertEquals("toList should keep nulls as nulls",
null, jsonArrayAsList.get(0));
}
}
33 changes: 33 additions & 0 deletions src/test/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.json.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -1816,6 +1817,38 @@ public void equals() {
aJsonObject.equals(aJsonObject));
}

/**
* Exercise the JSONObject toMap() method
*/
@Test
public void toMap() {
assertEquals("empty JSONObjects should return 0 length maps",
0, new JSONObject().toMap().size());

String str = "{" +
"\"nullKey\":null," +
"\"stringKey\":\"hello world!\"," +
"\"arrayKey\":[1, 2, 3]," +
"\"objectKey\":{\"subKey\": \"subValue\"}" +
"}";
JSONObject jsonObject = new JSONObject(str);
Map<String, Object> jsonObjectAsMap = jsonObject.toMap();
assertEquals("JSONObject and maps should contains the same number of entries",
jsonObject.length(), jsonObjectAsMap.size());
assertEquals("nulls should remain null",
null, jsonObjectAsMap.get("nullKey"));
assertEquals("strings should remain strings",
"hello world!", jsonObjectAsMap.get("stringKey"));
assertEquals("arrays should be converted to lists of same size",
3, ((List) jsonObjectAsMap.get("arrayKey")).size());
assertEquals("arrays should be converted to lists with same content",
1, ((List) jsonObjectAsMap.get("arrayKey")).get(0));
assertEquals("objects should be converted to maps of same size",
1, ((Map) jsonObjectAsMap.get("objectKey")).size());
assertEquals("objects should be converted to maps with same content",
"subValue", ((Map) jsonObjectAsMap.get("objectKey")).get("subKey"));
}

/**
* JSON null is not the same as Java null. This test examines the differences
* in how they are handled by JSON-java.
Expand Down