XStream – Basic Built-in Converters

In the previous tutorials we saw an example of how to convert java object to XML and back. That tutorial also explained the concept of aliases and implicit collection.In the last tutorial we show how to write a custom converter. In this tutorial, lets see some of the basic built in converters of XStream and how the resultant XML from those converters look like. We will look at how the following types are converted. XStream has converters for each of these. These are just a sample of the converters that XStream has. These converters only exist for a better and faster handling of a type compared to the generic converters.

  • BigDecimal
  • BigInteger
  • boolean
  • byte
  • char
  • Date
  • Double
  • Float
  • int
  • Long
  • Object
  • StringBuffer
  • StringBuilder
  • URI
  • URL
  • UUID

In the next tutorial we look at examples of arrays and Collections.

package com.studytrails.xml.xstream;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;
import com.thoughtworks.xstream.converters.basic.DateConverter;

public class ConverterExample1 {

	public static void main(String[] args) throws MalformedURLException, URISyntaxException {
		ConverterExample1 converter = new ConverterExample1();
		converter.convert();
	}

	private void convert() throws MalformedURLException, URISyntaxException {

		BasicConverterFields basicConverterExample = new BasicConverterFields();
		basicConverterExample.initialize();

		XStream xStream = new XStream();

		xStream.registerLocalConverter(BasicConverterFields.class, "flag", BooleanConverter.BINARY);

		DateConverter dateConverter = new DateConverter("yyyy-MM-dd HH:mm:ss", new String[] {}, TimeZone.getTimeZone("UTC"));
		xStream.registerConverter(dateConverter);

		String xml = xStream.toXML(basicConverterExample);

		System.out.println(xml);
		
		BasicConverterFields basicConverterFromXML = (BasicConverterFields) xStream.fromXML(xml);
		System.out.println(basicConverterFromXML);
		
	}
	
	class BasicConverterFields {
		BigDecimal bigDecimal = new BigDecimal(10000000000.0);
		BigInteger bigInteger = new BigInteger("1000000000");
		boolean flag = true;
		byte byteA = 'a';
		char charA = 'a';
		Date date = new Date();
		Double doubleA = new Double(1000000000000.0);
		Float floatA = new Float(10000000000000f);
		int intA = 100;
		Long longA = new Long(100000);
		Object nullA = null;
		Short shortA = new Short((short) 1);
		StringBuffer stringBufferA = new StringBuffer("test");
		StringBuilder stringBuilderA = new StringBuilder().append("test");
		URI UriA = null;
		URL urlA = null;
		UUID uuidA = UUID.fromString("0000000a-000b-000c-000d-00000000000e");

		public void initialize() throws URISyntaxException, MalformedURLException {
			UriA = new URI("file://C/work/fileA");
			urlA = new URL("http://www.google.com");
		}

		@Override
		public String toString() {
			return "BasicConverterFields [bigDecimal=" + bigDecimal + ", bigInteger=" + bigInteger + ", flag=" + flag + ", byteA=" + byteA
					+ ", charA=" + charA + ", date=" + date + ", doubleA=" + doubleA + ", floatA=" + floatA + ", intA=" + intA + ", longA=" + longA
					+ ", nullA=" + nullA + ", shortA=" + shortA + ", stringBufferA=" + stringBufferA + ", stringBuilderA=" + stringBuilderA
					+ ", UriA=" + UriA + ", urlA=" + urlA + ", uuidA=" + uuidA + "]";
		}

	}

}

The toXML method prints this


		10000000000
		1000000000
		1
		97
		a
		2014-04-26 04:14:38
		1.0E12
		9.9999998E12
		100
		100000
		1
		test
		test
		file://C/work/fileA
		http://www.google.com
		0000000a-000b-000c-000d-00000000000e
		

The fromXML method prints this

BasicConverterFields [bigDecimal=10000000000, bigInteger=1000000000, flag=true, byteA=97, charA=a, 
date=Sat Apr 26 09:44:38 IST 2014, doubleA=1.0E12, floatA=9.9999998E12, intA=100, longA=100000, 
nullA=null, shortA=1, stringBufferA=test, stringBuilderA=test, UriA=file://C/work/fileA, 
urlA=http://www.google.com, uuidA=0000000a-000b-000c-000d-00000000000e]

Leave a Comment