I’m hitting a problem in JSON I’m getting back from a Bugzilla server because it sometimes returns “text” : {} and sometimes “text” : “blah blah blah”. Bugzilla returns the former if no description was given for a bug. I’m mystified why it doesn’t come back as the much more sensible “text” : “” but it does and that’s it.
If I have a String named text in the target object for Gson, it objects when it sees the {} case because it says that’s an object and not a String:
Exception in thread "main" com.google.gson.JsonParseException: The
JsonDeserializer StringTypeAdapter failed to deserialized json object {} given
the type class java.lang.String
Any suggestions on how I can make Gson parse this?
Gson requires custom deserialization for the situation in the original question. Following is one such example.
input.json:
Foo.java:
Output:
Using instead a custom deserializer for the
Thing.classtype would of course be possible. This would have the benefit of not adding additional processing for everyString, but then you’d be stuck with “manual” processing all of the other attributes ofThing.