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

Support retaining of XML attribute-ness with `JsonNode` #614

Open DenisDolgalev opened 8 months ago

DenisDolgalev commented 8 months ago

Hi! I've faced an issue working with XML documents (deserialization, modifying, serialization): from JsonNode I can't serialize XML with attributes. Attributes become regular JsonNode fields after deserialization and then serialized as child tags. Kindly asking to consider adding new Feature (both for deserialization and serialization, disabled by default) to mark attribute field names with special prefix symbol (for example ampersand or underscore if ampersand may break something) during deserialization and, if new Feature enabled, serialize fields with this prefix to XML as attributes of parent tag (removing prefix before serialization), not as child tags.

cowtowncoder commented 8 months ago

I think that ability to "decorate" attribute names at streaming level -- and not just for JsonNode -- would probably be useful thing. I probably won't have time to do work on this right now, but could help someone if they wanted to.

Support, if any, should probably be enabled/disabled with 2 new features: one for reading (FromXmlParser.Feature) and another for writing (ToXmlGenerator.Feature).

I guess this should work something like:

  1. When reading, attribute names would be mutated to have leading @, exposed for JsonToken.FIELD_NAME
  2. When writing, if (and only if) JsonToken.FIELD_NAME being written has @ prefix, that'd be stripped and value output as attribute.

I suspect there would be some challenges due to use of incremental generator: all attributes need to be written before any elements. So not 100% sure it would actually work as expected.

But reader side seems simple enough.

alturkovic commented 6 months ago

I am also very interested in this feature. +1

Also, if there is a property with multiple attributes and a text, it would be great to group them together and put a new key for the actual text of the property. I think this tool does a great job at converting XML to JSON while preserving attribute/text when necessary.