Search code examples
javaserializationjacksondeserializationobjectmapper

How to use different JSONProperty on Serialize & Deserialize using Jackson API?


I had a Java object "Author" which was then restructured to become an Ararylist of "Author". Author object was saved in DB directly as JSON below:

{"author":{"id":1,"recordId":0}}

So my earlier Java Field was :

private Author author = new Author();

and new is :

private List<Author> authorList;

Problem is how i can write my code to have Serialized object with authorList but need to deserialize old "Author" as well.

I used @JsonProperty to read already saved author data but this also saves Arraylist named "Author" which i need to name as authorList

@JsonProperty(value="author")
@JsonDeserialize(using = AuthorDeserializer.class)

Solution

  • Googling around i have found solution for it. We can use @JsonAlias using latest Jackson API (2.9.7) So in my case i wanted this alias for deserialization @JsonAlias(value={"author","authorList"}).

    JSON Jackson parse different keys into same field