Skip to content
Advertisement

Different names of JSON property during serialization and deserialization

Is it possible: to have one field in class, but different names for it during serialization/deserialization in Jackson library?

For example, I have class “Coordiantes”.

JavaScript

For deserialization from JSON want to have format like this:

JavaScript

But when I will serialize object, result should be like this one:

JavaScript

I tried to implement this by applying @JsonProperty annotation both on getter and setter (with different values):

JavaScript

but I got an exception:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field “red”

Advertisement

Answer

Just tested and this works:

JavaScript

The idea is that method names should be different, so jackson parses it as different fields, not as one field.

Here is test code:

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement