New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Casting list of JSONObject to JSONObject throws ClassCastException #491
Comments
The public void shouldCastArrayOfJSONObjectsToList() {
String a = "{\"a\": 1}";
String b = "{\"b\":2}";
String jsonString = "{\"list\":[" + a + "," + b + ",8" + "]}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("list");
List<Object> objects = jsonArray.toList();
assertEquals(objects.size(), 3); // list of Map, Map, Integer
} For your specific example, your public void shouldCastArrayOfJSONObjectsToList() {
String a = "{\"a\": 1}";
String b = "{\"b\":2}";
String jsonString = "{\"list\":[" + a + "," + b + "]}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("list");
List<Map<String,Object>> objects = jsonArray.toList().stream().map(Map.class::cast).collect(Collectors.toList());
assertEquals(objects.size(), 2); // list of Map, Map
} However, If you really want public void shouldCastArrayOfJSONObjectsToList() {
String a = "{\"a\": 1}";
String b = "{\"b\":2}";
String jsonString = "{\"list\":[" + a + "," + b + "]}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("list");
List<JSONObject> objects = StreamSupport.stream(jsonArray.spliterator(), false).map(o -> {
if(o instanceof JSONObject) {
return (JSONObject)o;
}
return null;
}).filter(Objects::nonNull)
.collect(Collectors.toList());;
assertEquals(objects.size(), 2); // list of Map, Map
} The last method prevents a lot of work that the |
Thanks for your response @johnjaylward . Is it expected that the JSONArray iterator and spliterator methods behave differently than the toList method? If this is expected maybe the documentation could add some more clarification.
Could it be saying instead:
This clarifies what the output of the method is. I have created #492 to address this. Thanks |
Fixed in #492 |
@ggavriilidis sorry for the late reply. You are correct that the The The JSONArray arr = new JSONArray();
// populate the array with some data ... somehow
// standard iteration of a populated JSONArray
for (int i = 0; i<arr.length(); i++) {
Object o = arr.opt(i);
// do something with my object
}
// the above loop is the same as this:
Iterator i = arr.iterator();
while (i.hasNext()) {
Object o = i.next();
// do something with my object
}
// or the same as this for Java 8+
StreamSupport.stream(arr.spliterator(), false).forEach(o -> {
// do something with my object
}); In each case, above, the different iteration methods are all getting the same values for This differs wildly from the |
When calling the toList() method of a JSONArray which contains JSONObject the returned list contains HashMap elements.
The documentation mentions:
However, the elements are not converted to List.
Casting to JSONObject throws an exception if the toList() method is used.
Below is an example of the problem occurring:
Steps to reproduce
Run the below unit test:
Expected Behaviour
No exception should be thrown and the test should pass
Actual Behaviour
ClassCastException is thrown.
Using iterator works
The following test however passes:
Environment Information:
Operating System: macOS Sierra v 10.12.6
JDK Version: 1.8.0_152
org.json version: 20190722
The text was updated successfully, but these errors were encountered: