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

@JacksonXmlText cannot retrieve a wrapped text from a valid XML #574

Closed forceporquillo closed 1 year ago

forceporquillo commented 1 year ago

Given a POJO, I should be able to deserialize any element value from an XML element.

suppose, this object:

@Getter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class MatchingGroups {

   ...

   @JacksonXmlText
   private String content;
}

When deserializing a value from an XML element, note: that the value from that element is wrapped inside an XML element.

<MatchingGroups1 id="123" format="foo"><test>1</test></MatchingGroups1>

The expected value of matchingGroups.getContent() should be <test>1</test> however, in my case it was empty.

I am aware that the docs say: Allows specifying that value of one property is to be serialized as "unwrapped" text, and not in an element.

I am using the 2.13.4 version. Is there a way how we can easily achieve retrieving a wrapped text from an XML element?

cowtowncoder commented 1 year ago

No, that's not how XML Text works -- you cannot arbitrarily claim to not parse XML elements and leave them as "general XML content". If there is element <test>, it must match structure; XML parser will (and must) decode it separately into elements, text segments and so on.

Put another way, @JacksonXmlText can only map XML text segments (text within element), not for XML sub-trees.

You could bind it into

 private Object content;

or

private JsonNode content;

in which case contents would be read into Maps or JsonNode.

forceporquillo commented 1 year ago

Thanks for your clarifications and insights! I was able to resolve it by following your suggestion and reading the contents via JsonNode. Closing this issue.

cowtowncoder commented 1 year ago

@forceporquillo I am glad work-around works for you -- good luck!