Skip to content

Instantly share code, notes, and snippets.

@talfco
Last active May 26, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/59c5b6374c82d67b316dd5bf9c0e54e6 to your computer and use it in GitHub Desktop.
Save talfco/59c5b6374c82d67b316dd5bf9c0e54e6 to your computer and use it in GitHub Desktop.
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