Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private String convertAvroBinaryToJSON(byte[] msg) throws IOException {
long msgFingerprint = getAvroFingerprint(msg);
Schema mgsSchema = registry.getSchema(msgFingerprint);
byte[] payload = extractPayload(msg);
DatumReader<Object> reader = null;
if (msgFingerprint == this.backendComponentSchemaFingerprint)
reader = new GenericDatumReader<>(mgsSchema);
else
reader = new GenericDatumReader<>(mgsSchema,this.backendComponentSchema);
DatumWriter<Object> writer = new GenericDatumWriter<>(mgsSchema);
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(new ByteArrayInputStream(payload), null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(mgsSchema, outputStream, true);
Object datum = null;
while (!binaryDecoder.isEnd()) {
datum = reader.read(datum, binaryDecoder);
writer.write(datum, jsonEncoder);
jsonEncoder.flush();
}
outputStream.flush();
return outputStream.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment