Reputation: 67
How to unwrap more than one Map to JSON. Example
public class Class {
Map<String,String> firstMap;
Map<String,String> secondMap;
}
I can use @JsonAnyGetter only for one Map field. I know also that I can use custom Serializer, but I've got more fields in class for which I don't want to change the deserialization method. Preferable JSON output:
{
"Name": "Name",
"LastName": "LastName",
"firstMapKey": "firstMapValue"
"secondMapKey": "secondMapValue"
}
Instead of:
{
"Name": "Name",
"LastName": "LastName",
"firstMap": {
"firstMapKey": "firstMapValue"
},
"secondMap": {
"secondMapKey": "secondMapValue"
}
}
Upvotes: 0
Views: 921
Reputation: 67
@JonK helped me: I've added one additional Map and merge both Maps into it. For the additional map, I used @JsonAnyGetter and @JsonIgnore for both Maps used to merge.
public class Class {
@JsonIgnore
Map<String,String> firstMap;
@JsonIgnore
Map<String,String> secondMap;
Map<String,String> compositeMap
@JsonAnyGetter
public Map<String, String> getCompositeMap() {
return compositeMap;
}
@JsonAnySetter
public void setCompositeMap(Map<String, String> compositeMap) {
this.compositeMap = compositeMap;
}
}
Upvotes: 3