Deserialize JSON: Array sometimes NULL

0
I a JSON response I have an Array as follows: "colors": [ "Blue", "Black", "Green" ], I can deserialize this just fine using the Primitive solution from the REST Module. However sometimes a product has no colors and the JSON response looks like this: "colors": null, Is there a way for me to handle this? I get an error telling me that 'Colors' is not an Array (because it is empty). Is there any adjustment that can be made to the code to skip these parts, or does that cause other problems.
asked
1 answers
1

Yes you can. Take a look at JsonDeserializer.java around line 115. The section that has a comment:

//ReferenceSet

could be wrapped in something like this:

if (!object.isNull(attr) && object.getJSONArray(attr).length() > 0) {

which would cause the deserializer to skip that null attribute and continue on.

answered