Java – How can I use different JSONProperty for serialization and deserialization using the Jackson API?

How can I use different JSONProperty for serialization and deserialization using the Jackson API?… here is a solution to the problem.

How can I use different JSONProperty for serialization and deserialization using the Jackson API?

I have a Java object “Author” and then reorganize it into an Ararylist of “Author“. The author object is saved directly in the database in JSON format as follows:

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

So my previous Java Field was:

private Author author = new Author();

What’s new is:

private List<Author> authorList;

The problem is how do I write code to serialize an object with an authorList, but also need to deserialize the old “Author“.

I

use @JsonProperty to read the saved author data, but this also saves an Arraylist named “Author”, which I need to name authorList

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

Solution

Googling I found the solution.
We can use @JsonAlias using the latest Jackson API (2.9.7)
So in my case, I want this deserialized alias @JsonAlias(value={"author","authorList"}).

JSON Jackson parse different keys into same field

Related Problems and Solutions