Over a million developers have joined DZone.

Converting Java Objects to Byte Array, JSON and XML

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Converting a Java object (a process known as serialization) to various forms such as XML, JSON, or a byte array and back into java objects is a very common requirement. This post is intended to be a quick reference for you to easily make these conversions.

Java Object to Byte Array and Back

Lets start by converting a java object into a byte array and back. The library we will be using to achieve this result is the commons lang library which you can get using the following maven dependency.

<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>

The following two functions convert a java object to a byte array and back to java object respectively. You will need the following import:

import org.apache.commons.lang.SerializationUtils;


/**
* Convert object to byte array
* @param object
* @return
*/
public static byte[] fromJavaToByteArray(Serializable object) {
	return SerializationUtils.serialize(object);
}

/**
* Convert byte array to object
* @param bytes
* @return
*/
public static Object fromByteArrayToJava(byte[] bytes) {
	return SerializationUtils.deserialize(bytes);
}

Java Object to JSON and Back

Next, we will convert a java object to JSON and back to java. We will use the Jackson library for this. Use the following maven dependency.

<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.12</version>
</dependency>

The functions that perform the conversions.

/**
* Convert object to JSON String 
* @param object
* @return
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static String fromJavaToJson(Serializable object)
	throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper jsonMapper = new ObjectMapper();
    return jsonMapper.writeValueAsString(object);
}

/**
* Convert a JSON string to an object
* @param json
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static Object fromJsonToJava(String json, Class type) throws JsonParseException,
		JsonMappingException, IOException {
     ObjectMapper jsonMapper = new ObjectMapper();
     return jsonMapper.readValue(json, type);
}

Java Object to XML and Back

We will use Xstream from thoughtworks to serialize to XML. Include the following maven dependency.

<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
	<version>1.4.4</version>
</dependency>

And the one-liners to perform the conversions.

/**
* Convert a java object to XML
* @param object
* @return
*/
public static String fromJavaToXML(Serializable object) {
	XStream xs = new XStream();
	return xs.toXML(object);
}
	
/**
* Convert from XML to object
* @param xml
* @return
*/
public static Object fromXMLToJava(String xml){
	XStream xs = new XStream();
	return xs.fromXML(xml);
}





Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Faheem Sohail, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}