Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: 1.2.2
-
Fix Version/s: 1.3
-
Component/s: Converters, Core
-
Labels:None
-
JDK version and platform:Sun 1.5.0_11 for Windows
Description
Below is the minimal case for the problem I came across. During deserialization NPE is thrown from compareTo method, probably because object is put in the tree before id is filled. Simple workaround is to change the order of the fields, but sometimes it's not possible (for example when field is inherited).
import java.util.Set;
import java.util.TreeSet;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
public class Test extends TestCase {
XStream xs = new XStream();
public void test()
{ C c = new C(1); c.others.add(c); c.others.add(new C(2)); xs.fromXML(xs.toXML(c)); }}
class C implements Comparable {
Set<C> others = new TreeSet<C>();
final Integer id;
C(Integer id)
{ this.id = id; }public int compareTo(Object o)
{ return id.compareTo(((C) o).id); }}
Since XStream 1.2.2 you can set the order in which the fields are saved (see FAQ). Starting with 1.3 the inherited fields will be saved first (see announcement of 1.2.2 release).