Search in sources :

Example 6 with JsonDecoder

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;
}
Also used : JsonDecoder(org.apache.avro.io.JsonDecoder) EndOfFileException(voldemort.serialization.json.EndOfFileException) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) StringReader(java.io.StringReader) JsonReader(voldemort.serialization.json.JsonReader) IOException(java.io.IOException)

Aggregations

JsonDecoder (org.apache.avro.io.JsonDecoder)6 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)5 IOException (java.io.IOException)3 Map (java.util.Map)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Schema (org.apache.avro.Schema)2 Utf8 (org.apache.avro.util.Utf8)2 JsonReader (voldemort.serialization.json.JsonReader)2 StoreNotFoundException (com.sleepycat.persist.StoreNotFoundException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 AbstractMap (java.util.AbstractMap)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1