There is a problem that using XMLMapper.getValue(InputStream,Map.class) to deserializing property list
See original GitHub issueHere 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:
- Created 10 years ago
- Comments:5 (3 by maintainers)
Top 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 >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
The first reason is that you did not specify actual type of values for
Map
orList
. Instead ofList.class
, you need to use something likebecause 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.
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.