use of org.apache.avro.io.JsonDecoder in project voldemort by voldemort.
the class VoldemortClientShell method parseObject.
public static Object parseObject(SerializerDefinition serializerDef, String argStr, MutableInt parsePos, PrintStream errorStream) {
Object obj = null;
try {
// TODO everything is read as json string now..
JsonReader jsonReader = new JsonReader(new StringReader(argStr));
obj = jsonReader.read();
// mark how much of the original string, we blew through to
// extract the avrostring.
parsePos.setValue(jsonReader.getCurrentLineOffset() - 1);
if (StoreDefinitionUtils.isAvroSchema(serializerDef.getName())) {
// TODO Need to check all the avro siblings work
// For avro, we hack and extract avro key/value as a string,
// before we do the actual parsing with the schema
String avroString = (String) obj;
// From here on, this is just normal avro parsing.
Schema latestSchema = Schema.parse(serializerDef.getCurrentSchemaInfo());
try {
JsonDecoder decoder = new JsonDecoder(latestSchema, avroString);
GenericDatumReader<Object> datumReader = new GenericDatumReader<Object>(latestSchema);
obj = datumReader.read(null, decoder);
} catch (IOException io) {
errorStream.println("Error parsing avro string " + avroString);
io.printStackTrace();
}
} else {
// all json processing does some numeric type tightening
obj = tightenNumericTypes(obj);
}
} catch (EndOfFileException eof) {
// can be thrown from the jsonReader.read(..) call indicating, we
// have nothing more to read.
obj = null;
}
return obj;
}
Aggregations