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

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

Introduction

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

Prototype

public void endArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Usage

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.  ja  v a 2  s .co  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();/*from   w w w. j  av a 2  s  .  co m*/
    while (reader.peek() != JsonToken.END_ARRAY) {
        result.add(readJson(reader, elementSchema));
    }
    reader.endArray();
    return result;
}

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 ww. ja  va  2  s  .  c  om*/
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.
 *//*from   w  ww .  j a v  a2  s. 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.
 *//*from w w  w  . java  2 s .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;
}

From source file:co.cask.common.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 java.io.IOException When fails to construct a valid schema from the input.
 *//*from   w w w  .  ja va 2s  . com*/
private Schema readUnion(JsonReader reader, Set<String> knownRecords) throws IOException {
    ImmutableList.Builder<Schema> unionSchemas = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        unionSchemas.add(read(reader, knownRecords));
    }
    reader.endArray();
    return Schema.unionOf(unionSchemas.build());
}

From source file:co.cask.common.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 java.io.IOException When fails to construct a valid schema from the input.
 *//*from  w ww  .j  a va  2s .  c  om*/
private Schema readEnum(JsonReader reader) throws IOException {
    if (!"symbols".equals(reader.nextName())) {
        throw new IOException("Property \"symbols\" missing for enum.");
    }
    ImmutableList.Builder<String> enumValues = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        enumValues.add(reader.nextString());
    }
    reader.endArray();
    return Schema.enumWith(enumValues.build());
}

From source file:co.cask.common.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 java.io.IOException When fails to construct a valid schema from the input.
 *//*  w  w w  . j  a  v  a  2 s.c o m*/
private Schema readRecord(JsonReader reader, Set<String> 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.");
    }

    knownRecords.add(recordName);

    ImmutableList.Builder<Schema.Field> fieldBuilder = ImmutableList.builder();
    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();
    return Schema.recordOf(recordName, fieldBuilder.build());
}

From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java

License:Apache License

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();/*from  w  w w  . j  a va2  s .c  o m*/
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new ArrayList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read(in));
        }
        in.endArray();
        return list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read(in));
        }
        in.endObject();
        return map;

    case STRING:
        return in.nextString();

    case NUMBER: {
        String value = in.nextString();
        if (value.indexOf('.') >= 0) {
            return Double.parseDouble(value);
        } else {
            return Long.parseLong(value);
        }
    }

    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return null;

    default:
        throw new IllegalStateException();
    }
}

From source file:com.aliakseipilko.flightdutytracker.utils.AirportCodeConverter.java

License:Open Source License

private void generateAirports() {
    JsonReader jsonReader = new JsonReader(
            new InputStreamReader(ctx.getResources().openRawResource(R.raw.airports)));
    try {/*from   w w  w .  j  a  v  a2 s. c o m*/
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            Airport airport = gson.fromJson(jsonReader, Airport.class);
            if (airport.getIATA() == null) {
                airport.setIATA("");
            }
            if (airport.getICAO() == null) {
                airport.setICAO("");
            }
            airports.add(airport);
        }
        jsonReader.endArray();
        jsonReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}