Example usage for com.google.gson.stream JsonReader peek

List of usage examples for com.google.gson.stream JsonReader peek

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader peek.

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

From source file:co.cask.cdap.common.stream.StreamEventTypeAdapter.java

License:Apache License

@Override
public StreamEvent read(JsonReader in) throws IOException {
    long timestamp = -1;
    Map<String, String> headers = null;
    ByteBuffer body = null;//from   ww w .  j  a v a  2 s. c  o  m

    in.beginObject();
    while (in.peek() == JsonToken.NAME) {
        String key = in.nextName();
        if ("timestamp".equals(key)) {
            timestamp = in.nextLong();
        } else if ("headers".equals(key)) {
            headers = mapTypeAdapter.read(in);
        } else if ("body".equals(key)) {
            body = ByteBuffer.wrap(Bytes.toBytesBinary(in.nextString()));
        } else {
            in.skipValue();
        }
    }

    if (timestamp >= 0 && headers != null && body != null) {
        in.endObject();
        return new StreamEvent(headers, body, timestamp);
    }
    throw new IOException(String.format("Failed to read StreamEvent. Timestamp: %d, headers: %s, body: %s",
            timestamp, headers, body));
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static byte[] readBytes(JsonReader reader) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(128);
    reader.beginArray();//from   w w  w. j a v  a 2  s.c o m
    while (reader.peek() != JsonToken.END_ARRAY) {
        os.write(reader.nextInt());
    }
    reader.endArray();
    return os.toByteArray();
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static List<Object> readArray(JsonReader reader, Schema elementSchema) throws IOException {
    List<Object> result = new ArrayList<>();
    reader.beginArray();/*w w w  .j  a  v  a 2s . c o  m*/
    while (reader.peek() != JsonToken.END_ARRAY) {
        result.add(readJson(reader, elementSchema));
    }
    reader.endArray();
    return result;
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static Map<Object, Object> readMap(JsonReader reader, Map.Entry<Schema, Schema> mapSchema)
        throws IOException {
    Schema keySchema = mapSchema.getKey();
    if (!keySchema.isCompatible(Schema.of(Schema.Type.STRING))) {
        throw new IOException("Complex key type not supported: " + keySchema);
    }/* w w  w  . jav a  2 s .  c o m*/

    Schema valueSchema = mapSchema.getValue();
    Map<Object, Object> result = new HashMap<>();

    reader.beginObject();
    while (reader.peek() != JsonToken.END_OBJECT) {
        Object key = convertKey(reader.nextName(), keySchema.getType());
        result.put(key, readJson(reader, valueSchema));
    }
    reader.endObject();

    return result;
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static StructuredRecord readRecord(JsonReader reader, Schema schema) throws IOException {
    StructuredRecord.Builder builder = StructuredRecord.builder(schema);

    reader.beginObject();/*from   ww w . ja va  2s .  co  m*/
    while (reader.peek() != JsonToken.END_OBJECT) {
        Schema.Field field = schema.getField(reader.nextName());
        if (field == null) {
            // Ignore unrecognized fields
            reader.skipValue();
            continue;
        }

        builder.set(field.getName(), readJson(reader, field.getSchema()));
    }
    reader.endObject();

    return builder.build();
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static Object readUnion(JsonReader reader, Schema unionSchema) throws IOException {
    JsonToken token = reader.peek();
    // Based on the token to guess the schema
    for (Schema schema : unionSchema.getUnionSchemas()) {
        if (SCHEMA_TO_JSON_TYPE.get(schema.getType()) == token) {
            return readJson(reader, schema);
        }//from  ww  w.j  a va2 s  . co  m
    }

    throw new IOException("No matching schema found for union type: " + unionSchema);
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Reads json value and convert it into {@link Schema} object.
 *
 * @param reader Source of json/*from  ww w . j a va  2s  . c  o  m*/
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} reflecting the json.
 * @throws IOException Any error during reading.
 */
private Schema read(JsonReader reader, Map<String, Schema> knownRecords) throws IOException {
    JsonToken token = reader.peek();
    switch (token) {
    case NULL:
        return null;
    case STRING: {
        // Simple type or know record type
        String name = reader.nextString();
        if (knownRecords.containsKey(name)) {
            Schema schema = knownRecords.get(name);
            /*
               schema is null and in the map if this is a recursive reference. For example,
               if we're looking at the inner 'node' record in the example below:
               {
                 "type": "record",
                 "name": "node",
                 "fields": [{
                   "name": "children",
                   "type": [{
                     "type": "array",
                     "items": ["node", "null"]
                   }, "null"]
                 }, {
                   "name": "data",
                   "type": "int"
                 }]
               }
             */
            return schema == null ? Schema.recordOf(name) : schema;
        }
        return Schema.of(Schema.Type.valueOf(name.toUpperCase()));
    }
    case BEGIN_ARRAY:
        // Union type
        return readUnion(reader, knownRecords);
    case BEGIN_OBJECT: {
        reader.beginObject();
        String name = reader.nextName();
        if (!"type".equals(name)) {
            throw new IOException("Property \"type\" missing.");
        }
        Schema.Type schemaType = Schema.Type.valueOf(reader.nextString().toUpperCase());

        Schema schema;
        switch (schemaType) {
        case ENUM:
            schema = readEnum(reader);
            break;
        case ARRAY:
            schema = readArray(reader, knownRecords);
            break;
        case MAP:
            schema = readMap(reader, knownRecords);
            break;
        case RECORD:
            schema = readRecord(reader, knownRecords);
            break;
        default:
            schema = Schema.of(schemaType);
        }
        reader.endObject();
        return schema;
    }
    }
    throw new IOException("Malformed schema input.");
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#UNION UNION} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} of type {@link Schema.Type#UNION UNION}.
 * @throws IOException When fails to construct a valid schema from the input.
 *//*from w  w  w  .j a  v  a2 s. c o  m*/
private Schema readUnion(JsonReader reader, Map<String, Schema> knownRecords) throws IOException {
    List<Schema> unionSchemas = new ArrayList<>();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        unionSchemas.add(read(reader, knownRecords));
    }
    reader.endArray();
    return Schema.unionOf(unionSchemas);
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}.
 * @throws IOException When fails to construct a valid schema from the input.
 *///  w  w  w .j a v  a 2s  . c  o  m
private Schema readEnum(JsonReader reader) throws IOException {
    if (!"symbols".equals(reader.nextName())) {
        throw new IOException("Property \"symbols\" missing for enum.");
    }
    List<String> enumValues = new ArrayList<>();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        enumValues.add(reader.nextString());
    }
    reader.endArray();
    return Schema.enumWith(enumValues);
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}.
 * @throws IOException When fails to construct a valid schema from the input.
 *//*  w ww .  j  a v  a2s . c o m*/
private Schema readRecord(JsonReader reader, Map<String, Schema> knownRecords) throws IOException {
    if (!"name".equals(reader.nextName())) {
        throw new IOException("Property \"name\" missing for record.");
    }

    String recordName = reader.nextString();

    // Read in fields schemas
    if (!"fields".equals(reader.nextName())) {
        throw new IOException("Property \"fields\" missing for record.");
    }

    /*
      put a null schema schema is null and in the map if this is a recursive reference.
      for example, if we are looking at the outer 'node' reference in the example below,
      when we get to the inner 'node' reference, we need some way to know that its a record type
      and not a Schema.Type.
      {
        "type": "record",
        "name": "node",
        "fields": [{
          "name": "children",
          "type": [{
    "type": "array",
    "items": ["node", "null"]
          }, "null"]
        }, {
          "name": "data",
          "type": "int"
        }]
      }
      the full schema will be put in at the end of this method
    */
    knownRecords.put(recordName, null);

    List<Schema.Field> fieldBuilder = new ArrayList<>();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        reader.beginObject();
        if (!"name".equals(reader.nextName())) {
            throw new IOException("Property \"name\" missing for record field.");
        }
        String fieldName = reader.nextString();
        fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords)));
        reader.endObject();
    }
    reader.endArray();
    Schema schema = Schema.recordOf(recordName, fieldBuilder);
    knownRecords.put(recordName, schema);
    return schema;
}