Skip to content

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

Closed
@danblack

Description

@danblack

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 []
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions