Last active
May 12, 2021 13:41
-
-
Save jonmcewen/aa0319d1765ea1670f5aabfc3aa2a01c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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