question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialization of an empty list (with empty XML tag) results in `null`

See original GitHub issue

XML serialization/deserialization of an empty array is not working consistently (unlike JSON) . When I try to deserialize an empty list from xml I get a null list (but I except to get an empty list).
I don’t have this problem with JSON.

Library version 2.4.3, jdk 8.0_25 Here is my example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    @XmlRootElement(name = "root")
    public static class TestList {
        @XmlElement(name = "list")
        public List<Object> list;
    }

    public static void main(String[] args) throws IOException {
        // XML
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.registerModule(new JaxbAnnotationModule());
        TestList originalObject = new TestList();
        originalObject.list = new ArrayList<Object>();
        String xml = xmlMapper.writeValueAsString(originalObject);
        System.out.println(xml); // print <root xmlns=""><list></list></root>
        TestList actualObject = xmlMapper.readValue(xml, TestList.class);
        System.out.println(actualObject.list);  // print null

        // JSON
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JaxbAnnotationModule());
        String json = objectMapper.writeValueAsString(originalObject);
        System.out.println(json); // print {"list":[]}
         actualObject = objectMapper.readValue(json, TestList.class);
        System.out.println(actualObject.list); // print []
    }
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
kmp3325commented, Jun 10, 2016

Just ran into this problem myself…If you reverse the annotations it seems to work without issue:

    @JacksonXmlElementWrapper(localName = "person")
    @JacksonXmlProperty(localName = "people")
    public List<Person> people;
3reactions
cowtowncodercommented, Jan 15, 2020

@henrik242 Unfortunately that is bit different problem.

However! There is actually a way to make it work by using (relatively) new “config overrides” with null-handling override. Specifically, adding this:

       xmlMapper.configOverride(List.class)
            .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));

will then convert logical null value into “empty”, and for Lists that would be empty List.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson collection deserializer to accept empty xml tags
As a user of XML Jackson library, I would like to know if there is any possibility to deserialize XML List containing null...
Read more >
Jackson Ignore Null and Empty value - Chen Riang's Blog
Today, I bump into a problem when fixing a unit test when asserting Jackson XML deserialized string. Problem. Jackson XML deserialize null and ......
Read more >
Jackson Ignore Null and Empty Fields on Serialization
In this quick tutorial, I show you how to set up Jackson to ignore null or empty fields when serializing a Java class....
Read more >
Jackson 2.12: improved XML module - cowtowncoder - Medium
Empty elements (esp. for POJOs) as null s ... root value — it is now possible to serialize most scalar values as root...
Read more >
differentiating between null and empty string in a xml ... - MSDN
I have a xml which i Deserialize, using XMLSerializer class, into a custom object. One property of the custom object is a string...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found