Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 748
usman Avatar asked Mar 22 '26 11:03

usman


1 Answers

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

like image 120
usman Avatar answered Mar 25 '26 00:03

usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!