jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JsonObject.getArray VS Object[]

rednoah opened this issue · comments

One of the recent updates changed how JS arrays are parsed. Previously it used to be JsonObject instances, but now it's just Object[] which so I've got to fix a lot of ClassCastException issues.

Shouldn't JsonObject.getArray and JsonObject.isArray be completely @Depricated at this point? Or is there still a use case for those?

Recently, support for USE_MAPS was added to JsonReader.jsonToJava(). When USE_MAPS = true, the return value is a generic JsonObjects (Maps) where objects would be. The root of the JSON is the same as the original type of root object (e.g., string, double, long, null, etc. or Map if the root was an Object).

With this change, JsonReader.jsonToMaps() is no longer needed and will be deprecated. All jsonToMaps does now, is to ensure that the return value is always a Map. When this change was made, it did eliminate the need for the JsonObject.getArray() and JsonObject.isArray() methods in almost all cases. At this point, the only time you would use .getArray() or .isArray() would be if the root element of your object graph was an Collection type (ArrayList for example). In that case, in order to ensure that the right type is returned, the root element will have @type='arrayList' with "@Items":[item 1, item 2, ...].

In the example above, if you had called JsonReader.jsonToJava(json), then the ArrayList would be returned and no big deal. But, if you had called JsonReader.jsonToJava(json, [(JsonReader.USE_MAPS):true]), then a JsonObject would be returned that represents the root ArrayList object. You would call .getArray() to fetch it's items.

For brevity, I am using Groovy syntax for a Map of args above.

Thanks for the help. I understand how it works now. :)