Details
-
Type: New Feature
-
Status: Closed
-
Priority: Major
-
Resolution: Duplicate
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
Description
The use pattern of XStream looks simple and great.
It seems to miss the ability to serialize objects that have loops in the object references. A simple mechanism would be to insert refid's into the generated XML and access an existing instance of that object when further uses are encountered.
The main problem arises due to the fact that when serializing the object for the first time it is not known ahead if it will be referenced again. Either it would need:
a) a two pass form, where first the objects are scanned and then written out (bad idea)
b) the XML is written into a DOM, which is then updated when a reference is encountered.
c) offsets in the output XML string are help and the refid is inserted when encountered.
Could such a feature find it's way into XStream? Maybe enabled through a configuration switch?
Cheers,
Christoph
The way to do it :
IdentityHashMap map = new IdentityHashMap(){
{ Integer size = new Integer(size()); put( object, size); return size }public int putObject(Object object)
}
...
// some for loop iterating over the fields to be serialised
Object valueForField = fields[i].getValue();
if( valueForField == null ) // write out specific xml
else
{ int value = map.putObject(valueForField); // write xml with a reference }if( map.containsValue(valueForField) )
// write out a reference to the field, such as <person idref="45"/>
int value = ((Integer)map.get(valueForField)).intValue();
// write xml
else