FasterXML / jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Apache License 2.0
562 stars 221 forks source link

how to keep attribute order? #553

Closed yidou120 closed 1 year ago

yidou120 commented 1 year ago

I use @JsonOrder,but attribute that use attribute is true cannot order correctly. this annotation only useful for property field.

cowtowncoder commented 1 year ago

Attributes should be output in specific order according to @JsonPropertyOrder, although in XML attribute order has no semantic meaning. Do you have an example case showing different ordering?

yidou120 commented 1 year ago

this is pojo defined.

@JacksonXmlRootElement(localName = "person")
@Data
public class Person {
    @JacksonXmlProperty(isAttribute = true)
    private String email;
    @JacksonXmlProperty(isAttribute = true, localName = "person_name")
    private String name;
    @JacksonXmlProperty(isAttribute = true)
    private int age;

    private Adress adress;
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "friend")
    private List<Friend> friendList;
}
yidou120 commented 1 year ago

image this is output, age attribute is front of name.

yidou120 commented 1 year ago

I use @JsonPropertyOrder(value = {"email, name, age"}) this annotation, it seems not useful.

cowtowncoder commented 1 year ago

Maybe try using person_name instead of age for the annotation? (both should work, ideally, but maybe there's something preventing use of original name before renaming).

And yes, @JsonPropertyOrder should work.

One thing to make sure is that Lombok (since that is used I think?) does not remove any annotations on generated classes.

yidou120 commented 1 year ago

yes, I tried to use person_name instead of name, but order still wrong. and lombok does not remove any annotations on generated classes. image

cowtowncoder commented 1 year ago

@yidou120 Your problem is here:

I use 

@JsonPropertyOrder(value = {"email, name, age"})

this annotation, it seems not useful.

where you accidentally have single String, instead of 3 Strings like so:

@JsonPropertyOrder(value = {"email", "name", "age"})