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:com.ibm.og.util.json.type.CaseInsensitiveEnumTypeAdapterFactory.java

License:Open Source License

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

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            out.value(value.toString().toLowerCase(Locale.US));
        }

        @Override
        @SuppressWarnings("unchecked")
        public T read(final JsonReader in) throws IOException {
            final String s = in.nextString().toUpperCase(Locale.US);
            for (final Object enumEntry : rawType.getEnumConstants()) {
                if (enumEntry.toString().equals(s)) {
                    return (T) enumEntry;
                }
            }
            throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s));
        }
    }.nullSafe();
}

From source file:com.ibm.og.util.json.type.SizeUnitTypeAdapter.java

License:Open Source License

@Override
public SizeUnit read(final JsonReader in) throws IOException {
    return Units.size(in.nextString());
}

From source file:com.ibm.og.util.json.type.TimeUnitTypeAdapter.java

License:Open Source License

@Override
public TimeUnit read(final JsonReader in) throws IOException {
    return Units.time(in.nextString());
}

From source file:com.ibm.watson.developer_cloud.alchemy.v1.model.TypedEntitiesAdapter.java

License:Open Source License

@Override
public List<TypedEntity> read(JsonReader reader) throws IOException {
    List<TypedEntity> es = new ArrayList<TypedEntity>();

    reader.beginArray(); // arguments
    while (reader.hasNext()) {
        reader.beginObject(); // argument
        while (reader.hasNext()) {
            String name = reader.nextName();
            if ("entities".equals(name)) {
                reader.beginArray();//  ww  w.  j  a  v a2 s .c o  m
                while (reader.hasNext()) {
                    TypedEntity e = new TypedEntity();
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String name1 = reader.nextName();
                        if ("text".equals(name1)) {
                            e.setText(reader.nextString());
                        } else if ("type".equals(name1)) {
                            e.setType(reader.nextString());
                        } else if ("id".equals(name1)) {
                            e.setId(reader.nextString());
                        } else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                    es.add(e);
                }
                reader.endArray();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    reader.endArray();

    return es;
}

From source file:com.ibm.watson.developer_cloud.alchemy.v1.util.ImageKeywordTypeAdapter.java

License:Open Source License

@Override
public ImageKeyword read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from  www.j  a v  a2s.  c  om
        return null;
    }

    final ImageKeyword ImageKeyword = new ImageKeyword();
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();

        if (name.equals("text")) {
            final String text = reader.nextString();
            ImageKeyword.setText(text);
        } else if (name.equals("score")) {
            final String score = reader.nextString();
            if (score != null && !score.isEmpty())
                ImageKeyword.setScore(Double.valueOf(score));
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return ImageKeyword;
}

From source file:com.ibm.watson.developer_cloud.alchemy.v1.util.PublicationDateTypeAdapter.java

License:Open Source License

@Override
public PublicationDate read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/* w  ww.j a v  a 2s . c o m*/
        return null;
    }

    final PublicationDate publicationDate = new PublicationDate();
    publicationDate.setConfident(true);
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();

        if (name.equals("confident")) {
            final String confidentAsString = reader.nextString();
            publicationDate.setConfident(confidentAsString == null || !confidentAsString.equals("no"));
        } else if (name.equals("date")) {
            final String dateAsString = reader.nextString();
            if (dateAsString != null && !dateAsString.isEmpty())
                try {
                    publicationDate.setDate(df.parse(dateAsString));
                } catch (final ParseException e) {
                    log.log(Level.SEVERE, "Error parsing: " + dateAsString, e);
                }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return publicationDate;
}

From source file:com.ibm.watson.developer_cloud.alchemy.v1.util.TaxonomyTypeAdapter.java

License:Open Source License

@Override
public Taxonomy read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from   w w  w. j av a  2  s . co m
        return null;
    }

    final Taxonomy taxonomy = new Taxonomy();
    taxonomy.setConfident(true);

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

        if (name.equals("confident")) {
            final String confidentAsString = reader.nextString();
            taxonomy.setConfident(confidentAsString == null || !confidentAsString.equals("no"));
        } else if (name.equals("label")) {
            final String label = reader.nextString();
            taxonomy.setLabel(label);
        } else if (name.equals("score")) {
            final Double score = reader.nextDouble();
            taxonomy.setScore(score);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return taxonomy;
}

From source file:com.ibm.watson.developer_cloud.conversation.v1.model.util.PaginationResponseTypeAdapter.java

License:Open Source License

@Override
public PaginationResponse read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from  w ww. jav a2 s  . c  o  m*/
        return null;
    }
    reader.beginObject();
    PaginationResponse pagination = new PaginationResponse();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(REFRESH_URL)) {
            pagination.setRefreshUrl(reader.nextString());
        } else if (name.equals(NEXT_URL)) {
            String nextUrl = reader.nextString();
            HttpUrl url = HttpUrl.parse(RequestUtils.DEFAULT_ENDPOINT + nextUrl);
            pagination.setCursor(url.queryParameter(CURSOR));
            pagination.setNextUrl(nextUrl);
        } else if (name.equals(TOTAL)) {
            pagination.setTotal(reader.nextLong());
        } else if (name.equals(MATCHED)) {
            pagination.setMatched(reader.nextLong());
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    return pagination;
}

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechTimestampTypeAdapter.java

License:Open Source License

@Override
public SpeechTimestamp read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();// w  w  w  . j  a v  a 2  s . com
        return null;
    }

    final SpeechTimestamp speechTimestamp = new SpeechTimestamp();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechTimestamp.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setStartTime(reader.nextDouble());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechTimestamp.setEndTime(reader.nextDouble());
    }

    reader.endArray();
    return speechTimestamp;
}

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechWordConfidenceTypeAdapter.java

License:Open Source License

@Override
public SpeechWordConfidence read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*from   w  w w  .  ja  va2 s  . c o m*/
        return null;
    }

    final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechWordConfidence.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechWordConfidence.setConfidence(reader.nextDouble());
    }

    reader.endArray();
    return speechWordConfidence;
}