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.

There is a problem that using XMLMapper.getValue(InputStream,Map.class) to deserializing property list

See original GitHub issue

Here is my maven dependency

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.2</version>
    </dependency>

Here is my xml file.

<?xml version="1.0" encoding="UTF-8"?>
<vendor>
    <location id="store101">
        <address>
            <province>S.T</province>
            <street>Orchard Road</street>
        </address>
    </location>

    <location id="store102">
        <address>
            <province>S.T</province>
            <street>Tangerine Drive</street>
        </address>
    </location>
</vendor>

and here is my java code

package com.jackson.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;







import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.DeserializationFeature;;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.util.ArrayList;


public class UnitTest {

    public static void main(String[] args) {
        new UnitTest().testReadXmlToBean();
    }

    public void testReadXmlToBean() {
        JacksonXmlModule jacksonXmlModule = new JacksonXmlModule();
        jacksonXmlModule.setDefaultUseWrapper(false);
        ObjectMapper xmlMapper = new XmlMapper(jacksonXmlModule);

        InputStream is=null;
        String path="";
//      path="/META-INF/persistence.xml";
        path="/META-INF/Vendor.xml";
         is=this.getClass().getResourceAsStream(path);
        File file = new File(this.getClass().getResource(path).getPath());
        try {
            // JsonNode jsonNode=xmlMapper.readTree(is);
            Object obj= null;
//          obj = xmlMapper.readValue(is, Vendor.class);
//          obj = xmlMapper.readValue(is, List.class);
            obj = xmlMapper.readValue(is, Map.class);
            /*JsonNode jsonNode = xmlMapper.readTree(file);
            String value=jsonNode.path("persistence-unit[name]").asText();
            System.out.println(value);*/
            ObjectMapper jsonMapper = new ObjectMapper();
            String json = jsonMapper.writeValueAsString(obj);
            System.out.println(json);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            is.close();
        } catch (IOException e) {
        }
    }

}


class Vendor {
    private List<Location> location = new ArrayList<Location>();

    public List<Location> getLocation() {
        return location;
    }

    public void setLocation(List<Location> location) {
        this.location = location;
    }

}
class Location {
    private Adress address;
    private String id;

    public Adress getAddress() {
        return address;
    }

    public void setAddress(Adress address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}
class Adress {
    private String province;
    private String street;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}

I tried all of this (see below).Only the first one can get the correct result.Why?

//1
obj = xmlMapper.readValue(is, Vendor.class);
//2
obj = xmlMapper.readValue(is, List.class);
//3
obj = xmlMapper.readValue(is, Map.class);
//4
JsonNode jsonNode = xmlMapper.readTree(file);

I don’t want to create bean class.How can I deserialize property list to a Map and List object?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
cowtowncodercommented, Mar 12, 2014

The first reason is that you did not specify actual type of values for Map or List. Instead of List.class, you need to use something like

List<Location> list = xmlMapper.readValue(is, new TypeReference<List<Location>>() { });
// or:
static class LocationList extends ArrayList<Location> { }
LocationList list = xmlMapper.readValue(is, LocationList.class);

because otherwise you are just asking for List<Object>.

Map would not work since structure is not compatible with maps.

As to tree model (JsonNode); XML and JSON are structurally incompatible, and use of tree model is not fully supported with XML module. It does work for some cases but not all; so I generally suggest not using it with XML. There are XML-specific tree models (DOM, Dom4j, JDOM) that work better.

Hope this helps.

0reactions
cowtowncodercommented, Mar 14, 2014

Ok no problem. This is just bit of a FAQ, I should write down better explanation. It is not at all unreasonable to wish for smoother handling; especially given that this works just fine for JSON. Unfortunately XML imposes more limitations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't deserialize a wrapped List with Jackson XmlMapper
This is a known bug in Jackson's XML handling. A work-around is to change the annotation on inners in the Outer constructor so...
Read more >
XML Serialization and Deserialization with Jackson - Baeldung
This short tutorial shows how the Jackson library can be used to serialize Java object to XML and deserialize them back to objects....
Read more >
FasterXML/jackson-databind - Gitter
The issue is that while deserializing and serializing a container with an abstract property which has subtype info works, having a container with...
Read more >
Simple 2.7.1 - Simple XML Serialization
Here the XML element and attribute names have been overridden with the annotation ... It declares a list with the entry class as...
Read more >
How to get value from object Json.deserialize
It 'll help if you include the code for your MyObjects class. The answer is probably along the lines of "the same way...
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