Jackson library: establishing order of a json file when using object mapper

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"}]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật