Deserialization of an empty list (with empty XML tag) results in `null`
See original GitHub issueXML 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:
- Created 9 years ago
- Reactions:1
- Comments:22 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just ran into this problem myself…If you reverse the annotations it seems to work without issue:
@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:
will then convert logical
null
value into “empty”, and forList
s that would be emptyList
.