Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonPropertyOrder annotation for JSON formatting #4387

Closed
1 task done
Jerry-X07 opened this issue Feb 15, 2024 · 6 comments
Closed
1 task done

@JsonPropertyOrder annotation for JSON formatting #4387

Jerry-X07 opened this issue Feb 15, 2024 · 6 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@Jerry-X07
Copy link

Jerry-X07 commented Feb 15, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I have a Java class named OrderCount which is annotated with @JsonPropertyOrder from the package com.fasterxml.jackson.annotation.JsonPropertyOrder. The goal is to format the JSON output in a specific order. The desired order is: entityId, entityName, totalTests, products, and childEntities at the end. However, despite attempting to reorder the properties, I'm encountering difficulties placing childEntities at the end of the JSON output.

@Data
@SuperBuilder(toBuilder = true)
@NoArgsConstructor
@JsonPropertyOrder({"entityId", "entityName", "totalTests", "products", "childEntities"})
public class OrderCount {
    private String entityId;
    private String entityName;
    private Integer totalTests;

    @Builder.Default
    private Map<String, Integer> products = new HashMap<>();

    private List<OrderCount> childEntities;

    @JsonAnyGetter
    public Map<String, Integer> getProducts() {
        return products;
    }

    @JsonAnySetter
    public void setAdditionalProductProperty(String name, Integer value) {
        products.put(name, value);
    }
}

Specifically, when I try to change the order of entityId, entityName, and totalTests, then it's working. However, when I try to change the order of products or childEntities, it's not working as expected

Version Information

No response

Reproduction

No response

Expected behavior

{ "entityId": "1", "entityName": "entity1", "totalTests": 6, "productName_1":2, "productName_2":2, . . "productName_n":2, "childEntities": [ { "entityId": "2", "entityName": "entity2", "totalTests": 3, "productName_1":1, "productName_2":1, . . "productName_n":1, "childEntities": [] }, { "entityId": "3", "entityName": "entity3", "totalTests": 3, "productName_1":1, "productName_2":1, . . "productName_n":1, "childEntities": [] } ] }

But Instead of above JSON Format getting This JSON format :
{ "entityId": "1", "entityName": "entity1", "totalTests": 6, "childEntities": [ { "entityId": "2", "entityName": "entity2", "totalTests": 3, "childEntities": [], "productName_1":1, "productName_2":1, . . "productName_n":1 }, { "entityId": "3", "entityName": "entity3", "totalTests": 3, "childEntities": [], "productName_1":1, "productName_2":1, . . "productName_n":1 } ], "productName_1":2, "productName_2":2, . . "productName_n":2 }

Additional context

No response

@Jerry-X07 Jerry-X07 added the to-evaluate Issue that has been received but not yet evaluated label Feb 15, 2024
@JooHyukKim
Copy link
Member

Maybe @JsonAnyGetter, @JsonAnySetter may be properties/getters/setters with those annotations are not respected....? 🤔

@yihtserns
Copy link
Contributor

yihtserns commented Feb 15, 2024

Can you share how the decompiled .class looks like?

I'm suspecting either @ConstructorProperties or MapperFeature.SORT_CREATOR_PROPERTIES_FIRST is the cause.

EDIT: What was I thinking... 🤦‍♂️

@cowtowncoder
Copy link
Member

@JsonPropertyOrder does not affect serialization of "any" properties output by @JsonAnyGetter; they are always serialized after actual POJO properties.

There is #518 requesting "any properties" to be sorted (at least wrt alphabetic sorting), but I don't think anyone is working on that. The issue is that output of any-properties is quite separate from output of regular properties so there is no easy fix.

But I guess the idea of using logical name of "any getter" accessor (here, products) for determining ordering of any-properties contained makes sense from user perspective.

@cowtowncoder
Copy link
Member

I filed #4388 to specifically request that @JsonPropertyOrder would have effect on grouped "any" and "unwrapped" properties.

@cowtowncoder
Copy link
Member

I assume #4388 is a replacement for this, so closing.

@cowtowncoder
Copy link
Member

@Jerry-X07 which version were you testing this against?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

4 participants