Reputation: 1391
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)
Upvotes: 3
Views: 3896
Reputation: 3423
You can also add this feature to your objectMapper:
DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY
Upvotes: 1
Reputation: 1391
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
Upvotes: 2
Reputation: 10931
If you only need to deserialize with the "author" property, the simplest way is just to give it the setter method it will be looking for.
For example:
@JsonProperty("author")
public void setAuthor(Author author) {
setAuthorList(new ArrayList<>(Collections.singletonList(author)));
}
Upvotes: 0