Re: [Simple-support] Deserilize objects with inheritance
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2012-08-07 05:02:58
|
add the @Attribute(name="id") to the constructor in B
--- On Mon, 6/8/12, Nhat Ngo <phu...@gm...> wrote:
From: Nhat Ngo <phu...@gm...>
Subject: [Simple-support] Deserilize objects with inheritance
To: sim...@li...
Received: Monday, 6 August, 2012, 7:27 PM
Hi everyone,
I'm a student, new to SimpleXML library. Per the title suggested, I am having a huge problem deserializing a stored inherited object using Java SimpleXML library. Let's say my codes are:@Root(name="a")
public class A {
@Attribute(name="id")
protected int id;
public A(@Attribute(name="id") int id){
this.id = id;
}
}
@Root(name="b")
public class B extends A {
@Element(name="key",type=int.class)
private int key;
public B(int id,
@Element(name="key",type=int.class) int key) {
super(id);
this.key = key;
}
}
public class Main {
public static void Main() {
File src = new File("example.xml");
Serializer serializer = new Persister();
//Serialize the object
B b = new B(1,1);
serializer.write(b, src);
//Error here, deserialize the object
B b2 = serializer.read(B.class, src);
}
}
There is no error and the Serializer happily serializes the object b to:<b id="1">
<key>1</key>
</b>
Now when I attempt to deserialize it, the program throws:org.simpleframework.xml.core.PersistenceException: Constructor not matched for class B
How should I annotate both class A and B to behave correctly? I've tried put in @Attribute(name="id") in B constructor to no avail and the serializer failed to serialize anything.
Thanks.
-----Inline Attachment Follows-----
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
-----Inline Attachment Follows-----
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
|