tech.allegro.schema.json2avro.converter.JsonAvroConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of converter Show documentation
Show all versions of converter Show documentation
JSON to Avro conversion tool designed to make migration to Avro easier
package tech.allegro.schema.json2avro.converter;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.io.NoWrappingJsonEncoder;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificRecord;
import org.apache.avro.specific.SpecificRecordBase;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class JsonAvroConverter {
private JsonGenericRecordReader recordReader;
public JsonAvroConverter() {
this.recordReader = new JsonGenericRecordReader();
}
public JsonAvroConverter(ObjectMapper objectMapper) {
this.recordReader = new JsonGenericRecordReader(objectMapper);
}
public JsonAvroConverter(ObjectMapper objectMapper, UnknownFieldListener unknownFieldListener) {
this.recordReader = new JsonGenericRecordReader(objectMapper, unknownFieldListener);
}
public byte[] convertToAvro(byte[] data, String schema) {
return convertToAvro(data, new Schema.Parser().parse(schema));
}
public byte[] convertToAvro(byte[] data, Schema schema) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
GenericDatumWriter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy