I have a springboot application, which among other things, uses object mapper from Jackson library to convert json arrays (that are in string format) to POJO and after some works on the said arrays uses the same mapper to convert them back to json strings. I want to make sure that my fields (within the json arrays) are in a certain order whenever I convert POJO back to json, for the sake of readability and consistency (as well as it being a requirement).
The demo for my app is as follows:
logic class
private final ObjectMapper mapper = new ObjectMapper();
public Map<String, JSONArray> doWork(List<String> jsonArrs) {
Set<DemoModel> outputArrs = new HashSet<>();
for (String arr : jsonArrs) {
DemoModel[] pojoArrs =
arr == null ? new DemoModel[] {} : mapper.readValue(arr, DemoModel[].class);
// Some work happens here, none of which modifies arrays or changes pojoArrs in any way
outputArrs.addAll(Set.of(pojoArrs));
}
return Map.of("myKey",new JSONArray(mapper.writeValueAsString(outputArrs)));
}
DemoModel class
@Data // from lombok dependency - it just creates getters/setters for me that is all
public class DemoModel {
@JsonProperty(value = "field3")
private AnotherModel field3;
@JsonProperty(value = "@field1") // sometimes this field will have @ before its name, hence the annotation
private String field1;
@JsonProperty(value = "@field2")
private String field2;
}
AnotherModel class
@Data
public class AnotherModel {
@JsonProperty(value = "@field1")
private String field1; // yes this field has the same name as field1 from DemoClass (in case relevant)
@JsonProperty(value = "@otherField2")
private String otherField2;
}
Now, a simple google search told me about @JsonPropertyOrder
annotation which does precisely what I need. So by adding this annotation on top of my DemoModel
class with a desired order, as so – @JsonPropertyOrder({"field1", "field2", "field3"})
, will do the trick. There are a number of examples here where it works – for example this article has a basic set up that seemed to work.
However, in my example it does not work. The order remains random, no matter what I try. I can see that the order is establish by adding the snippet of code suggested in this article. So I know for a fact that order is definitely being defined. But it seems, the order from my annotation is ignored after I convert my POJO array to string.
I found some evidence online, where using this annotation didn’t work for others too (this article for example faced the same issue) without having a solution.
I attempted to use index args of @JsonProperty
annotations that I have in my DemoModel
class, as suggested in this article , as well as my first one. But that didn’t work either.
So my question is as follows – what did I do wrong in either of the approaches I mentioned above? And what other alternatives are there to dictate the order of my fields within my json?
For reference, the desired output should be as follows:
[{"@field1": "val1","@field2": "val2","field3": {"@field1": "val3","@otherField2": "val4"}}]
But is as below instead:
[{"field3": {"@field1": "val3","@otherField2": "val4"},"@field1": "val1","@field2": "val2"}]