I’m writting a program in Java that makes requests to an external API by calling some methods. Each of these methods returns a diferent JSON structure and it also depends on the passed parameters. My question is if there’s some way to deserialize the response string using Gson without having to write a different class for each method.
If not Gson, which other library could I use?
Thank you.
Yes, there is. If the JSON structure that you don’t care to deserialize into a specific Java type is just an object that contains key-value pairs, where all of the values are JSON primitives, then the structure can be simply deserialized into a
Map<String, String>.Note that a
Map<String, Object>would not work as it does not provide enough type information for Gson to deserialize into. Gson would just create plainObjectinstances for each value — notStringorIntegerinstances, but just plainObjectinstances. The actual values in the JSON would not be deserialized. On the surface it seems reasonable that Gson could be enhanced to create more type-specific instances forJsonPrimitivevalues. For example, since it’s possible to determine whether aJsonPrimitiveis aBoolean, with a call toisBoolean(), then one might hope Gson would just construct aBoolean, but it doesn’t — Gson just creates an empty and probably-uselessObject.Here’s an example that demonstrates this point.
input.json Contents:
Foo.java:
The output of this example is
{3=false, one=won, too=22}.If the
Maptype in the example is changed toHashMap<String, Object>as recommended in another answer, then the output becomes{3=java.lang.Object@10b61fd1, one=java.lang.Object@24e2dae9, too=java.lang.Object@299209ea}, which is of course probably useless.If for some reason a type of
HashMap<String, Object>must be deserialized to, then custom deserialization processing would be necessary.If the JSON structure is not a simple set of key-value pairs, where the values are all JSON primitives, but include values that are complex types like JSON objects, even if it’s acceptable to transform all of the values into
Strings, then it would also be necessary to implement custom deserialization processing.Don’t hesitate to head on over to the Gson Issues List and submit an enhancement request for improved JSON primitive deserialization to appropriately specific Java
Objecttypes, or to have a simple mechanism to transform any value — including complex JSON types like objects and arrays — in a Map into aString, such that custom deserialization processing as described above wouldn’t be necessary.