XStream
Encyclopedia
XStream is a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 library to serialize
Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...

 objects
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 to XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

 (or JSON
JSON
JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

) and back again.

XStream library

XStream uses reflection
Reflection (computer science)
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime....

 to discover the structure of the object graph to serialize at run time, and doesn't require modifications to objects. It can serialize internal fields, including private and final, and supports non-public and inner classes.

Object graph serialization

When serializing an object it serializes the full object graph. Duplicate references encountered in the object-model will be maintained. For example using the following class CD

package com.thoughtworks.xstream;
public class Cd {
private String id;

private Cd bonusCd;

Cd(String id, Cd bonusCd) {
this.id = id;
this.bonusCd = bonusCd;
}

Cd(String id) {
this.id = id;
}

public String getId {
return id;
}

public Cd getBonusCd {
return bonusCd;
}
}

and add some of these object to a list

Cd bj = new Cd("basement_jaxx_singles");
Cd mr = new Cd("maria rita");

List order = new ArrayList;
order.add(mr);
// adds the same cd twice (two references to the same object)
order.add(bj);
order.add(bj);

// adds itself (cycle)
order.add(order);

XStream xstream = new XStream;
xstream.alias("cd", Cd.class);
System.out.println(xstream.toXML(order));

If the above code is executed with XStream's default relative references mode, it will generate the following XML:


maria rita


basement_jaxx_singles






XStream is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

, distributed under a permissive, revised BSD-style licence.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK