All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ssf4j.avro.json.AvroJsonSerialization Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
package org.ssf4j.avro.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.ssf4j.Serialization;

/**
 * Serialization facade that uses Apache avro's JSON encoder
 * 

* Note that avro can only reliably serialize objects created from * avro schemas. * @author robin * */ public class AvroJsonSerialization implements Serialization { private static Schema schema(Class type) { Object obj = null; if(type == boolean.class || type == Boolean.class) obj = true; else if(type == char.class || type == Character.class) obj = 'T'; else if(type == double.class || type == Double.class) obj = 1d; else if(type == float.class || type == Float.class) obj = 1f; else if(type == int.class || type == Integer.class) obj = 1; else if(type == long.class || type == Long.class) obj = 1L; else if(type == short.class || type == Short.class) obj = (short) 1; try { if(obj == null && type != null) obj = type.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } try { return GenericData.get().induce(obj); } catch (Exception e) { throw new RuntimeException(e); } } /** * {@inheritDoc} */ @Override public AvroJsonSerializer newSerializer(OutputStream out, Class type) throws IOException { return new AvroJsonSerializer(schema(type), out); } /** * {@inheritDoc} */ @Override public AvroJsonDeserializer newDeserializer(InputStream in, Class type) throws IOException { return new AvroJsonDeserializer(schema(type), in, type); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy