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

org.ssf4j.avro.json.AvroJsonDeserializer 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 org.apache.avro.Schema;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.specific.SpecificDatumReader;
import org.ssf4j.Deserializer;

public class AvroJsonDeserializer implements Deserializer {

	protected Schema schema;
	protected Class type;
	protected Decoder dec;
	protected InputStream in;
	
	public AvroJsonDeserializer(Schema schema, InputStream in, Class type) throws IOException {
		this.schema = schema;
		this.in = in;
		this.type = type;
		dec = DecoderFactory.get().jsonDecoder(schema, in);
	}
	
	@Override
	public T read() throws IOException {
		Object obj = new SpecificDatumReader(schema).read(null, dec);
		
		if(type == String.class)
			obj = obj.toString();
		
		return type.cast(obj);
	}

	@Override
	public void close() throws IOException {
		in.close();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy