How to avro binary encode my json string to a byte array? How to avro binary encode my json string to a byte array? json json

How to avro binary encode my json string to a byte array?


It seems to be working at least. It can be simplified though: the loops are useless since having more than one object would result in invalid JSON. Also, it could be a good idea to avoid unnecessary parsing of the schema by preparsing it.

Here's my version:

public class AvroTest {    private static final String JSON = "{" + "\"name\":\"Frank\"," + "\"age\":47" + "}";    private static final Schema SCHEMA = new Schema.Parser().parse("{ \"type\":\"record\", \"namespace\":\"foo\", \"name\":\"Person\", \"fields\":[ { \"name\":\"name\", \"type\":\"string\" }, { \"name\":\"age\", \"type\":\"int\" } ] }");    public static void main(String[] args) throws IOException {        byte[] data = jsonToAvro(JSON, SCHEMA);        String jsonString = avroToJson(data, SCHEMA);        System.out.println(jsonString);    }    /**     * Convert JSON to avro binary array.     *      * @param json     * @param schema     * @return     * @throws IOException     */    public static byte[] jsonToAvro(String json, Schema schema) throws IOException {        DatumReader<Object> reader = new GenericDatumReader<>(schema);        GenericDatumWriter<Object> writer = new GenericDatumWriter<>(schema);        ByteArrayOutputStream output = new ByteArrayOutputStream();        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, json);        Encoder encoder = EncoderFactory.get().binaryEncoder(output, null);        Object datum = reader.read(null, decoder);        writer.write(datum, encoder);        encoder.flush();        return output.toByteArray();    }    /**     * Convert Avro binary byte array back to JSON String.     *      * @param avro     * @param schema     * @return     * @throws IOException     */    public static String avroToJson(byte[] avro, Schema schema) throws IOException {        boolean pretty = false;        GenericDatumReader<Object> reader = new GenericDatumReader<>(schema);        DatumWriter<Object> writer = new GenericDatumWriter<>(schema);        ByteArrayOutputStream output = new ByteArrayOutputStream();        JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);        Decoder decoder = DecoderFactory.get().binaryDecoder(avro, null);        Object datum = reader.read(null, decoder);        writer.write(datum, encoder);        encoder.flush();        output.flush();        return new String(output.toByteArray(), "UTF-8");    }}