Jackson 2.12: improved XML module

Nested Lists in POJOs should work reliably (esp. unwrapped)

Empty elements (esp. for POJOs) as nulls

<root>
<value />
</root>
public class Root {
public MyValue value;
}

Root value deserialization works similar to property values

Integer value = Integer.valueOf(42);
String xml = new XmlMapper().writeValueAsString(value);
// xml == "<Integer>42</Integer>"
assertEquals(value, xml.readValue(xml, Integer.class);

Additional XML Attributes allowed for Scalars

<value>
<description xml:lang="en_US">Something</description>
</value>
@JacksonXmlRootElement
public class Value {
public String description;
}

May write xsi:nil (as well as read)

<root>
<value xsi:nil="true" />
</root>
XmlMapper xmlMapper = XmlMapper.builder()
.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true)
.build();

JsonNode/Object[] now support Repeated Elements (as Arrays)

<root>
<a>Foo</a>
<a>Bar</a>
</root>
{ "a" : "Foo",
"a" : "Bar"
}
{ "a" : [ "Foo", "Bar" ] }
Map<String, Object> stuff = xmlMapper.readTree(xml);
// will now have Lists if multiple elements with same name found

Some support for “Mixed Content”

<body>Hello, <b>world</b>!</body>
{ "b" : "world" }
{
"" : "Hello, ",
"b" : "world",
"" : "!"
}
  • it is not quite clear how this would map to POJO (no way to specify properties with no name)
  • if binding to JsonNode, order is not fully retained: content would look like:
{
"" : [ "Hello, "!" ],
"b" : "world"
}

Miscellaneous other fixes

Future Work

  1. “Attribute-ness” is not preserved by JsonNode or buffering (`TokenBuffer`) — ideally it should ([dataformat-xml#217])
  2. Property names may collide in cases where they should not: either due to namespace being ignored (i.e. local name collision occurs even if namespaces differ), or due to list-wrapper name not being used to avoid collisions, or even element-vs-attribute distinction
  3. Custom escaping of output not yet supported (see [dataformat-xml#75])
  4. Polymorphic type id can not be used as property name (“flattening”, see [dataformat-xml#197])
  5. Customizing output of document aspects: XML Schema attribute ([dataformat-xml#90]), DOCTYPE declaration ([dataformat-xml#150]), custom namespace prefixes ([dataformat-xml#207]), encoding other than “UTF-8” in XML declaration ([dataformat-xml#315]), use of xsi:type for output ([dataformat-xml#324])
  6. Handling of Maps, and especially Map keys, is… challenging, wrt XML name validity rules ([dataformat-xml#244])

--

--

--

Open Source developer, most known for Jackson data processor (nee “JSON library”), author of many, many other OSS libraries for Java, from ClassMate to Woodstox

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Announcing Flutter beta 1: Build beautiful native apps

How to set up SFTP server on Ubuntu(AWS-EC2)

A Guild to Beautiful Games

Infosys Interview Experience — 2021

Build an HTML Email Template From Scratch

Use Vagrant to Improve Development Environment Setup Time

Reading Analog Data in Zephyr with a nrf52840

How we test systems at OrbitSoft: functional, smoke, regression, and acceptance testing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
@cowtowncoder

@cowtowncoder

Open Source developer, most known for Jackson data processor (nee “JSON library”), author of many, many other OSS libraries for Java, from ClassMate to Woodstox

More from Medium

Let's Understand the Variables in Java

JAVA Volatile Variables

Log4Shell Shows The Need for “Trustworthy Java”

Numbers and statics