How to maintain order while using @JsonAnyGetter #196
-
public class CustomClass {
private String fieldOne;
private String fieldTwo;
private Map<String, String> firstMap;
private Map<String, String> secondMap;
private String fieldThree;
private String fieldFour;
} Hi, so I have a class which has two Maps. I want to serialize this class using Jackson, but I also want to unwrap the two maps. Now apparently public class CustomClass {
private String fieldOne;
private String fieldTwo;
private Map<String, String> firstMap;
private Map<String, String> secondMap;
private String fieldThree;
private String fieldFour;
// Constructors, getters, and setters
@JsonAnyGetter
public Map<String, String> getCombinedMaps() {
Map<String, String> combined = new LinkedHashMap<>();
combined.putAll(firstMap);
combined.putAll(secondMap);
return combined;
}
} So this works. Now the other constraint I have is that the order of the Json must be in the order that their declared in, i.e. {
"fieldOne" : ...,
"fieldTwo" : ...,
"Name": "Name", <- firstMap
"LastName": "LastName", <- firstMap
"firstMapKey": "firstMapValue" <- secondMap
"secondMapKey": "secondMapValue" <-secondMap
"fieldThree" : ...,
"fieldFour" : ...,
} So naturally, I thought to use @JsonPropertyOrder, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Correct, Plus, as you have noticed, only one can be specified for a given POJO. I cannot think of a way to achieve ordering currently, although issues requesting solving issues you outline have existed for a while:
The problem in solving these is the dynamic nature of "any properties" vs static nature of regular POJO properties, and sorting used. One way forward might be, I think, to give some sort of symbolic name to a set of "any properties" (perhaps via new property of I am not sure I will have time to pursue these, but they might help someone else contribute improvements. |
Beta Was this translation helpful? Give feedback.
-
I managed to solve both your issues (1. custom ordering and 2. multiple use of @JsonAnyGetter in one object) with the following approach, translated to the example of your input class:
I haven't tested this specific code, but I got the approach running in my own project. It works. |
Beta Was this translation helpful? Give feedback.
Correct,
@JsonAnyGetter
contents are not considered "true" properties (plus they are fully dynamic, not known a priori) so none of the ordering-annotations work with it.Plus, as you have noticed, only one can be specified for a given POJO.
I cannot think of a way to achieve ordering currently, although issues requesting solving issues you outline have existed for a while:
@JsonUnwrapped
for Maps) -- but I will actually fix this now due to@JsonAnyGetter
providing functionality.@JsonAnyGetter
with@JsonPropertyOrder
)The problem in solving these is the dynamic nature of "any properties" vs static nature of regular POJO properties, and sorting used.
One way forw…