XStream

Serialize superclass(es) before subclasses - boolean configuration property

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.2.2
  • Component/s: None
  • Labels:
    None

Description

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...

Activity

Hide
Geoffrey De Smet added a comment -

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."

Show
Geoffrey De Smet added a comment - 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."
Hide
Joerg Schaible added a comment -

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
Show
Joerg Schaible added a comment - 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
Hide
Geoffrey De Smet added a comment -

I 'll try it out.
1.3.0 will use this by default right?

Show
Geoffrey De Smet added a comment - I 'll try it out. 1.3.0 will use this by default right?
Hide
Joerg Schaible added a comment -

Yes, 1.3.x will do this by default.

Show
Joerg Schaible added a comment - Yes, 1.3.x will do this by default.
Hide
Geoffrey De Smet added a comment -

It works for me with:
xStream = new XStream(new PureJavaReflectionProvider(new FieldDictionary(new NativeFieldKeySorter())));

Great work

Show
Geoffrey De Smet added a comment - It works for me with: xStream = new XStream(new PureJavaReflectionProvider(new FieldDictionary(new NativeFieldKeySorter()))); Great work
Hide
Geoffrey De Smet added a comment -

Note that 1.4.1 still uses ImmutableFieldKeySorter by default:

public PureJavaReflectionProvider() {
		this(new FieldDictionary(new ImmutableFieldKeySorter()));
	}
Show
Geoffrey De Smet added a comment - Note that 1.4.1 still uses ImmutableFieldKeySorter by default:
public PureJavaReflectionProvider() {
		this(new FieldDictionary(new ImmutableFieldKeySorter()));
	}

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: