Skip to content

Instantly share code, notes, and snippets.

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);
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 =, binaryDecoder);
writer.write(datum, jsonEncoder);
return outputStream.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment