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

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

Introduction

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

Prototype

public String nextString() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#STRING string value of the next token, consuming it.

Usage

From source file:classifiers.DummyClassifier.java

License:Apache License

public void parseStreamAndClassify(String jsonFile, String resultsFile) throws IOException {

    String journalName;//from  w w w  . ja va2 s. c o m
    int count = 0;
    int abstract_count = 0;

    try {
        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile)));
        JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(resultsFile), "UTF-8"));
        writer.setIndent("    ");

        //reader.setLenient(true);
        reader.beginArray();
        writer.beginArray();
        while (reader.hasNext()) {

            reader.beginObject();
            writer.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if (name.equals("abstract")) {
                    abstract_count++;
                    reader.skipValue();

                } else if (name.equals("pmid")) {
                    String pmid = reader.nextString();
                    writer.name("labels");
                    writeLabels(writer);
                    writer.name("pmid").value(pmid);
                } else if (name.equals("title")) {
                    reader.skipValue();
                } else {
                    System.out.println(name);
                    reader.skipValue();
                }
            }
            reader.endObject();
            writer.endObject();
        }
        reader.endArray();
        writer.endArray();

        System.out.println("Abstracts: " + abstract_count);

        writer.close();
    } catch (FileNotFoundException ex) {

    }
}

From source file:cloud.artik.client.JSON.java

License:Apache License

@Override
public DateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:// www .j a  v  a 2 s  .  c  om
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        return formatter.parseDateTime(date);
    }
}

From source file:cn.ieclipse.af.demo.sample.volley.adapter.StringAdapter.java

License:Apache License

@Override
public String read(JsonReader reader) {
    String text = "";
    try {/*w  w  w.ja  va  2 s  .  c  o  m*/
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            text = "";//Null
        } else {
            text = reader.nextString();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return text;
}

From source file:co.cask.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory.java

License:Apache License

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    @SuppressWarnings("unchecked")
    Class<T> rawType = (Class<T>) type.getRawType();
    if (!rawType.isEnum()) {
        return null;
    }/* ww w .  j  av  a2s  . c o  m*/

    final Map<String, T> lowercaseToConstant = new HashMap<>();
    for (T constant : rawType.getEnumConstants()) {
        lowercaseToConstant.put(toLowercase(constant), constant);
    }

    return new TypeAdapter<T>() {
        public void write(JsonWriter out, T value) throws IOException {
            if (value == null) {
                out.nullValue();
            } else {
                out.value(toLowercase(value));
            }
        }

        public T read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            } else {
                return lowercaseToConstant.get(toLowercase(reader.nextString()));
            }
        }
    };
}

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;/*  w  ww . java 2s  .  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 Object readJson(JsonReader reader, Schema schema) throws IOException {
    switch (schema.getType()) {
    case NULL:/* w w w .  jav a  2s  . co  m*/
        reader.nextNull();
        return null;
    case BOOLEAN:
        return reader.nextBoolean();
    case INT:
        return reader.nextInt();
    case LONG:
        return reader.nextLong();
    case FLOAT:
        // Force down cast
        return (float) reader.nextDouble();
    case DOUBLE:
        return reader.nextDouble();
    case BYTES:
        return readBytes(reader);
    case STRING:
        return reader.nextString();
    case ENUM:
        // Currently there is no standard container to represent enum type
        return reader.nextString();
    case ARRAY:
        return readArray(reader, schema.getComponentSchema());
    case MAP:
        return readMap(reader, schema.getMapSchema());
    case RECORD:
        return readRecord(reader, schema);
    case UNION:
        return readUnion(reader, schema);
    }

    throw new IOException("Unsupported schema: " + schema);
}

From source file:co.cask.cdap.gateway.handlers.log.LogOffsetAdapter.java

License:Apache License

@Override
public LogOffset read(JsonReader in) throws IOException {
    return FormattedLogEvent.parseLogOffset(in.nextString());
}

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   w  w  w . j av a2 s.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#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 av a 2 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  www .j a va2 s.co  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;
}