Deserialize a JSON with keeping a value as String

56 views
Skip to first unread message

Johannes Paul

unread,
Jul 6, 2019, 4:20:14 AM7/6/19
to jackson-user
I would like to deserialize a JSON to an Object, which holds a Object named 'data'. I want everything to be stored as a string, to deserialize it later.
The reason is, i dont know the exact target class for the data, if the data arives, but to get the exact class, i first need the rest of the JSON deserialized.

I Posted a question on StackOverflow wich explains the issue a bit more and provides a working code example (except getter and setter).

Is there any chance to get the content ob a object/array as a String through the JsonParser in a JsonDeserializer?

Thanks in advance!

Tatu Saloranta

unread,
Jul 12, 2019, 1:13:34 AM7/12/19
to jackson-user
If you mean whether you can try get "raw" content of JSON values of any type as `java.lang.String`, no -- parser will decode content into tokens, and it would not make sense to then try to create JSON again.

But you don't have to do that anyway: instead, declare target as `JsonNode` (to get a tree representation), or either `Map` (if known to be JSON Object) or `Object`, and you will get "untyped" structure (Map, List, String, Number, Boolean). Either way you can then convert from that type into whatever you decide eventual target to be, based on other information. Conversion method to use is either `convertValue()` (with Map/Object), or `treeToValue()` (for `JsonNode`).

-+ Tatu +-
 

Reply all
Reply to author
Forward
0 new messages