History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-363
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Assignee: Unassigned
Reporter: Geoffrey De Smet
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
XStream

Serialize superclass(es) before subclasses - boolean configuration property

Created: 15/Nov/06 03:11 AM   Updated: 29/Aug/07 12:59 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.2.2


 Description  « Hide
See user mailing list "HashCode corrupted on deserialzing map"

Currently Xstream always serializes subclasses before superclasses. For example:

public class Person {
  public String name; //getters and setters
}
public class Employee extends Person {
  public String address; //getters and setters
}
becomes serialized as:
<Employee>
  <address>street 15</street>
  <name>John Smith</name>
</Employee>

Besides the unlogical aesthetic aspect, this corrupts maps in subclasses that use keys based on superclassses (see user list).

The solution would be a boolean configuration property that enabled/disables superclass serializing before subclass serializing.
I would even propose to make enable it by default and provide disabling for backwards compability...

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Geoffrey De Smet - 15/Nov/06 03:16 AM
Java serialization always serializing superclasses before subclasses:

http://www.oreilly.com/catalog/javarmi/chapter/ch10.html

"An important part of serialization involves writing out class-related metadata associated with an instance. Most instances are more than one class. For example, an instance of String is also an instance of Object. Any given instance, however, is an instance of only a few classes. These classes can be written as a sequence: C1, C2...CN, in which C1 is a superclass of C2, C2 is a superclass of C3, and so on. This is actually a linear sequence because Java is a single inheritance language for classes. We call C1 the least superclass and CN the most-derived class."

Joerg Schaible - 17/May/07 03:13 PM
Hi Geoffrey,

the latest SNAPSHOT version has already a FieldKeySorter interface. I've checked in now an implementation, that works as suggested. You give it a try. See the CustomFieldKeySorter acceptance test how XStream can be initialized with such a sorter and make use of the NativeFieldKeySorter implementation. Report back, if it works for you.

- Jörg

Geoffrey De Smet - 25/Aug/07 07:28 AM
I 'll try it out.
1.3.0 will use this by default right?

Joerg Schaible - 27/Aug/07 02:11 AM
Yes, 1.3.x will do this by default.

Geoffrey De Smet - 29/Aug/07 12:59 PM
It works for me with:
xStream = new XStream(new PureJavaReflectionProvider(new FieldDictionary(new NativeFieldKeySorter())));

Great work :)