Parsing json where field can have two different types with gson or jackson
I have json with field that contains two different types.
"fields":[{"value":"ZIELONE OKO"},{"value":{"@nil":"true"}}]
I have problem with deserializing these. My class w开发者_开发百科ith model contains:
private String value;
And I simply need to translate {"@nil":"true"} into null. Right now I get error:
The JsonDeserializer StringTypeAdapter failed to deserialized json object {"@nil":"true"} given the type class java.lang.String
Ideally, I would change code that produces that odd JSON: shouldn't second value just be JSON null? I guess it is being produced by some complex process, starting with XML (which must use 'isNul' to differentiate between null String and empty String).
But if that is not possible, I think both Jackson and Gson would require either custom deserializer; or to first bind to a generic Map and then handle value oddities explicitly. In second case, you just declare type to bind to as Map (possible with generic type info using reference; but that should be optional).
精彩评论