Details
-
Type: Bug
-
Status: Closed
-
Priority: Blocker
-
Resolution: Won't Fix
-
Affects Version/s: 1.3.1
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
JDK version and platform:JDK1.4 and j2re1.4.2_19
Description
Hi,
I am facing blocker issue with xstream your quick response is much appreciated.
I am using xstream for serialization/deserialization of between json and java. Json to java conversion is working fine with arrays. But the deserialization to java is throwing exception.Is this a known bug with xstream? is there any workaround?
I am using below libraries.Pasting the code below also attached the java files required.
1. xstream1.3.1.jar
2. jettison1.1.jar
3. stax-api1.0.1.jar
import com.restjson.Student;
import com.restjson.Subject;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
public class TestXstream
{
public static void main(String[] args)
{ testStudent(); //has problem with array of objects }private static void testStudent()
{ Student student=new Student(); student.setSid(12); student.setName("RAJ"); Subject[] subjects=new Subject[2]; subjects[0]=new Subject(1,"AAA"); subjects[1]=new Subject(2,"BBB"); student.setSubjects(subjects); String json = javaToJSON(student,Student.class); System.out.println("JSON:"+json); Student std = (Student)(JSONToJava(json,Student.class)); System.out.println("JAVA:"+std.getName()); System.out.println("Sub:"+std.getSubjects()[0].getSubName()); }private static String javaToJSON(Object object,Class clazz)
{ XStream xstream = new XStream(new JsonHierarchicalStreamDriver()); xstream.alias("root", clazz); return xstream.toXML(object); }private static Object JSONToJava(String json,Class clazz)
{ XStream xstream = new XStream(new JettisonMappedXmlDriver()); xstream.alias("root", clazz); return xstream.fromXML(json); }}
Generated JSON string below:
{"root": {
"sid": 12,
"name": "RAJ",
"subjects": [
,
{ "subName": "BBB", "subId": 2 } ]
}}
Exception trace below:
com.thoughtworks.xstream.converters.ConversionException: subName : subName : subName : subName
---- Debugging information ----
message : subName : subName
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : subName : subName
class : com.restjson.Student
required-type : [Lcom.restjson.Subject;
path : /root/subjects/subName
line number : -1
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
at json.test.TestXstream.JSONToJava(TestXstream.java:60)
at json.test.TestXstream.testStudent(TestXstream.java:86)
at json.test.TestXstream.main(TestXstream.java:66)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: subName : subName
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:71)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:86)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:96)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:52)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
at com.thoughtworks.xstream.converters.collections.ArrayConverter.unmarshal(ArrayConverter.java:55)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
Jettison 1.1 does not work with XStream, use Jettison 1.2. And please use the user's list next time first.