Skip to content

Instantly share code, notes, and snippets.

@jonmcewen
Last active May 12, 2021 13:41
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 jonmcewen/aa0319d1765ea1670f5aabfc3aa2a01c to your computer and use it in GitHub Desktop.
Save jonmcewen/aa0319d1765ea1670f5aabfc3aa2a01c to your computer and use it in GitHub Desktop.
private static <T extends SpecificRecord> T getSpecificRecord(
String json, Schema writersSchema, Class<T> recordClass)
throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Schema readersSchema = (Schema) recordClass.getMethod("getClassSchema").invoke(null);
Decoder jsonDecoder = DecoderFactory.get().jsonDecoder(writersSchema, json);
ResolvingDecoder resolvingDecoder = DecoderFactory.get().resolvingDecoder(writersSchema, readersSchema, jsonDecoder);
DatumReader<T> reader = new SpecificDatumReader<>(recordClass);
return reader.read(null, resolvingDecoder);
}
public MySpecificRecord deserialize(ByteBuffer byteBuffer) {
try {
BinaryMessageDecoder<MySpecificRecord> decoder =
MySpecificRecord.createDecoder(customSchemaStore);
return decoder.decode(byteBuffer);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to deserialize MySpecificRecord", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment