Handling unknown JSON properties with Jackson

For deserializing json with an unknown field in the object, there is @JsonAnySetter.

But what if I read such json into my object, change some known fields and write it back to json? Unknown properties will be lost.

How to handle such cases? Is it possible to map an object or do I need to read data in JsonNode or Map?

+4
source share
2 answers

Unmarshalling in a custom Java class has its advantages and disadvantages. This gives you a nice static typing, but it's good, static. Javadoc for @JsonAnySetter assumes that it is similar to JAXB @XmlAnyElement , but unlike @XmlAnyElement data objects do not contain name information, so this is a one-way street.

if you need to handle dynamic JSON streams, you need to bite the bullet and use Map or JsonNode .

+1
source

There is this RFE for Jackson: http://jira.codehaus.org/browse/JACKSON-292 to add such a function. Makes general sense when you think about it.

+2
source

Source: https://habr.com/ru/post/1308738/


All Articles