XStream

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
XStream Library
DeveloperCodehaus
Initial releaseJanuary 1, 2004; 22 years ago (2004-01-01)
Stable release
1.4.20 / December 24, 2022; 3 years ago (2022-12-24)
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Written inJava
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    Operating systemCross-platform
    LicenseBSD-style license
    Websitex-stream.github.io

    XStream is a Java library to serialize objects to XML (or JSON) and back again.

    NOTE: Not to confuse with XStream stream processing platform at Meta.

    XStream library

    [edit | edit source]

    XStream uses reflection 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.[1]

    Object graph serialization

    [edit | edit source]

    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<Cd> 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:

    <list>
      <cd>
        <id>maria rita</id>
      </cd>
      <cd>
        <id>basement_jaxx_singles</id>
      </cd>
      <cd reference="../cd[2]"/>
      <list reference=".."/>
    </list>
    

    XStream is free software, distributed under a permissive, revised BSD-style licence.

    Usage

    [edit | edit source]

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    [edit | edit source]

    Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').