use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.
the class AvroUtil method jsonFromGenericRecord.
public static String jsonFromGenericRecord(GenericRecord record) throws IOException {
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
Encoder jsonEncoder = avroAdapter.createJsonEncoder(record.getSchema(), outputStream);
writer.setSchema(record.getSchema());
writer.write(record, jsonEncoder);
jsonEncoder.flush();
return outputStream.toString();
}
use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.
the class AvroUtil method bytesFromGenericRecord.
public static byte[] bytesFromGenericRecord(GenericRecord record) throws IOException {
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
Encoder binaryEncoder = avroAdapter.createBinaryEncoder(outputStream);
writer.setSchema(record.getSchema());
writer.write(record, binaryEncoder);
binaryEncoder.flush();
return outputStream.toByteArray();
}
use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.
the class AvroUtil method genericRecordFromJson.
public static GenericRecord genericRecordFromJson(String json, Schema schema) throws IOException {
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
Decoder jsonDecoder = avroAdapter.createJsonDecoder(schema, json);
reader.setSchema(schema);
GenericRecord record = reader.read(null, jsonDecoder);
return record;
}
use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.
the class AvroUtil method genericRecordFromBytes.
public static GenericRecord genericRecordFromBytes(byte[] bytes, Schema schema) throws IOException {
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
Decoder binaryDecoder = avroAdapter.createBinaryDecoder(bytes);
reader.setSchema(schema);
GenericRecord record = reader.read(null, binaryDecoder);
return record;
}
Aggregations