Hello All
I am using betwixt to serialize/deserialize a data type structure and
found test case where readed object is not as expected. As i am a newbie
using betwixt I would appreciate if someone could tell me what i am doing
wrong.
I have wrote two sample classes to reproduce the issue in a simple way:
Class 'A' represents a list of registers type
Class 'B' represents a register with a map of fields (only of type 'A' for
this expamle)
And this is the main code executed
A rootA = new A();
rootA.setName("RootA");
B rootB = new B();
rootB.setName("rootB");
A a1 = new A();
a1.setName("A1");
A a2 = new A();
a2.setName("A2");
B b1 = new B();
b1.setName("B1");
rootA.setType(rootB);
rootB.addField("fieldX", a1);
a1.setType(b1);
// b1.addField("fieldY", a2);
storeObject(rootA);
A readedObject = readObject(rootA.getName()+".xml");
System.out.println(readedObject);
Which result is as expected:
RootA[(rootB:{fieldX=A1[(B1:{})]})]
But if I uncomment the line "b1.addField("fieldY", a2);" then the 'fieldX'
of object 'rootB' is missing and the result is:
RootA[(rootB:{})]
I have also noticed that with this same structure but with more objects not
only fields are lost but they are stored into
wrong objects.
XML generated:
<root className="A" name="RootA">
<type className="B" name="rootB">
<fields>
<entry>
<key>fielX</key>
<value className="A" name="A1">
<type className="B" name="B1">
<fields>
<entry>
<key>fieldY</key>
<value className="A" name="A2"/>
</entry>
</fields>
</type>
</value>
</entry>
</fields>
</type>
</root>
////////////////////////////////////// CLASS A
public class A {
public B type;
public String getClassName() {
return A.class.getName();
}
public void setType(B type) {
this.type = type;
}
public B getType() {
return this.type;
}
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return getName()+"["+(type!= null? type.toString(): "")+"]";
}
}
////////////////////////////////////// CLASS B
public class B{
public Map fields= new HashMap();
public Map getFields() {
return fields;
}
public void addField(String name, A value) {
fields.put(name, value);
}
public String getClassName() {
return B.class.getName();
}
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "("+getName()+":" + fields + ")";
}
}
Hope you can help me.
Thanks a lot for your help.
Jacobo Sánchez