I've below code which deserializes my code and returns a HashMap for Jettisons version 1.2.
But when I switched the jettisons to 1.5.3 this code breaks with the exception mentioned below.
Can someone please help?
--main.class
package org.example;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
Person person = new Person(22,"Nikhil","India");
Map<String,Object> map = new HashMap<>();
map.put("Person1",person);
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.allowTypes(new Class[]{Person.class});
//1003
String str = xStream.toXML(map);
System.out.println("toXML ==> " + str);
Map<String,Object> map1 = (Map<String,Object>)xStream.fromXML(str);
System.out.println("fromXML ==> " + map1);
}
}
--person.class
package org.example;
public class Person {
private int age;
private String name,address;
public Person(int age, String name, String address) {
this.age = age;
this.name = name;
this.address = address;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
--output for 1.2 jettisons
toXML ==> {"map":[{"entry":{"string":"Person1","org.example.Person":{"age":22,"name":"Nikhil","address":"India"}}}]}
fromXML ==> {Person1=org.example.Person@d4342c2}
--output for 1.5.3 jettisons
toXML ==> {"map":[{"entry":{"string":"Person1","org.example.Person":{"age":22,"name":"Nikhil","address":"India"}}}]}
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Cannot construct type
---- Debugging information ----
message : Cannot construct type
cause-exception : java.lang.InstantiationException
cause-message : java.util.Map$Entry
construction-type : java.util.Map$Entry
class : java.util.Map$Entry
required-type : java.util.Map$Entry
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /map/map/entry
line number : -1
class[1] : java.util.HashMap
required-type[1] : java.util.HashMap
converter-type[1] : com.thoughtworks.xstream.converters.collections.MapConverter
version : 1.4.20
-------------------------------
at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:89)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:580)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:276)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:52)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readBareItem(AbstractCollectionConverter.java:132)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:117)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readCompleteItem(AbstractCollectionConverter.java:147)
at com.thoughtworks.xstream.converters.collections.MapConverter.putCurrentEntryIntoMap(MapConverter.java:106)
at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:99)
at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:93)
at com.thoughtworks.xstream.converters.collections.MapConverter.unmarshal(MapConverter.java:88)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:52)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:136)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1464)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1441)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1321)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1312)
at org.example.Main.main(Main.java:23)
Caused by: java.lang.InstantiationException: java.util.Map$Entry
at java.base/jdk.internal.misc.Unsafe.allocateInstance(Native Method)
at jdk.unsupported/sun.misc.Unsafe.allocateInstance(Unsafe.java:839)
at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:85)
... 24 more
I've tried to ObjectMapper by Jackson, but it returns a LinkedHashMap while deserialization. I'm expecting a HashMap.