com.vladkrava.converter.AvroToXmlConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of avro-converter Show documentation
Show all versions of avro-converter Show documentation
Enables conversion of Apache Avro object into popular data formats: JSON, XML, etc
package com.vladkrava.converter;
import org.apache.avro.specific.SpecificRecord;
import org.json.JSONObject;
import org.json.XML;
import com.vladkrava.converter.serialization.AvroSerializer;
import com.vladkrava.converter.serialization.DataSerializationException;
/**
* Converting data from Avro's SpecificRecord object to XML string
*
* @author Vlad Krava - [email protected]
* @see SpecificRecord
* @see SourceConverter
* @see AvroSerializer
* @see XML
* @since 0.1-SNAPSHOT
*/
public class AvroToXmlConverter extends AvroSerializer implements SourceConverter {
/**
* Performs converting to XML string
*
* @param source - Avro's source object
* @return XML {@link String} on successful data serialization or null if Avro's source object is null
* @throws DataSerializationException - on any exception with serialization from source datatype to a byte array
*/
@Override
public String convert(final T source) throws DataSerializationException {
final byte[] json = serialize(source);
return json == null ? null : XML.toString(new JSONObject(new String(json)), source.getSchema().getName());
}
}