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
567 stars 221 forks source link

Can we serialize elements with pair? like elements of key-value pair. #601

Closed RahulKwani closed 1 year ago

RahulKwani commented 1 year ago

I am using Jackson XML annotation to serialize/deserialize xml output of my spring boot APIs. This API is meant to provide legacy support. Because of that, it's required to have the exact same response structure as the existing API.

I encountered a peculiar situation for which I could not find any solution. I tried customSerilizer but that also doesn't seem to solve the problem.

I need to serialize A and B tags into pairs without any parent tags. The existing API serializes data in this format:

<Item>
  <SNO>22656565</SNO>
  <Weight>0.0</Weight>
  <A>data1</A>
  <B>foo1</B>
  <A>data2</A>
  <A>data3</A>
  <B>foo3</B>
  <A>data4</A>
  <A>data5</A>
  <A>data6</A>
  <B>foo6</B>
</Item>

My pojo look like this:

@Data
@JacksonXmlRootElement(localName = "Item")
@JsonPropertyOrder({"SNO", "Weight", "A", "B"})
public class Item {

    @JacksonXmlProperty(localName = "SNO")
    private String sNo;

    @JacksonXmlProperty(localName = "Weight")
    private Float weight;

    @JacksonXmlProperty(localName = "A")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<String> aList;

    @JacksonXmlProperty(localName = "B")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<String> bList;
}

Can it be achived using this library? Whatever I try at the end all of A and B tags are grouped after serialization. Maybe some combination of annotations I haven't come across?

I had asked this same question on Stackoverflow: https://stackoverflow.com/questions/76512689/how-to-print-custom-sequence-of-tag-using-jackson-databind-lib-in-java

JooHyukKim commented 1 year ago

@RahulKwani Have you tried inquiry to jackson-dataformat-xml module? FYI, jackson-databind does not depend on xml module 👍🏻

cowtowncoder commented 1 year ago

There is no way to automate such conversions at this point.

RahulKwani commented 1 year ago

I was able to serilize using a custom serilizer, but it seems deserilization is still a headache. It be great if there is way to collect common tags () into a list. Something opposite of @JacksonXmlElementWrapper.

RahulKwani commented 1 year ago

Closed this now as this feature will not have much usage.