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: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  w  w  .  ja v a  2 s  .  com
        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();//from   w  w w  . j  ava  2  s.  c  o m
        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();/*  w  ww  . j  a  v  a  2 s  . c om*/
        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;
}

From source file:com.ibm.watson.developer_cloud.tradeoff_analytics.v1.util.ColumnTypeAdapter.java

License:Open Source License

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

    ColumnType type = ColumnType.TEXT;
    Goal goal = null;
    Boolean objective = null;

    String key = null, format = null, description = null, fullName = null, low = null, high = null;
    Double significantGain = null, significantLoss = null, insignificantLoss = null;

    List<String> categoricalRange = null, categoricalPreference = null;

    reader.beginObject();

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

        if (name.equals(TYPE2)) {
            type = ColumnType.fromString(reader.nextString());
        } else if (name.equals(KEY)) {
            key = reader.nextString();
        } else if (name.equals(GOAL)) {
            goal = Goal.fromString(reader.nextString());
        } else if (name.equals(IS_OBJECTIVE)) {
            objective = reader.nextBoolean();
        } else if (name.equals(FORMAT)) {
            format = reader.nextString();
        } else if (name.equals(DESCRIPTION)) {
            description = reader.nextString();
        } else if (name.equals(FULL_NAME)) {
            fullName = reader.nextString();
        } else if (name.equals(SIGNIFICANT_GAIN)) {
            significantGain = reader.nextDouble();
        } else if (name.equals(SIGNIFICANT_LOSS)) {
            significantLoss = reader.nextDouble();
        } else if (name.equals(INSIGNIFICANT_LOSS)) {
            insignificantLoss = reader.nextDouble();
        } else if (name.equals("preference")) {
            reader.beginArray();
            categoricalPreference = new ArrayList<String>();
            while (reader.hasNext()) {
                categoricalPreference.add(reader.nextString());
            }
            reader.endArray();
        } else if (name.equals(RANGE)) {
            if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) {
                reader.beginArray();
                categoricalRange = new ArrayList<String>();
                while (reader.hasNext()) {
                    categoricalRange.add(reader.nextString());
                }
                reader.endArray();
            } else {
                reader.beginObject();
                while (reader.hasNext()) {
                    name = reader.nextName();
                    if (name.equals(LOW)) {
                        low = reader.nextString();
                    } else if (name.equals(HIGH)) {
                        high = reader.nextString();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    Column column;
    if (type == ColumnType.CATEGORICAL) {
        column = new CategoricalColumn();
        if (categoricalRange != null) {
            ((CategoricalColumn) column).setRange(categoricalRange);
        }
        if (categoricalPreference != null) {
            ((CategoricalColumn) column).setRange(categoricalPreference);
        }
    } else if (type == ColumnType.DATETIME) {
        column = new DateColumn();
        if (low != null) {
            try {
                ((DateColumn) column).withRange(DATE_FORMATTER.parse(low), DATE_FORMATTER.parse(high));
            } catch (final ParseException e) {
                LOG.log(Level.SEVERE, "Error parsing the date", e);
            }
        }
    } else if (type == ColumnType.NUMERIC) {
        column = new NumericColumn();
        if (low != null) {
            ((NumericColumn) column).range(Double.valueOf(low), Double.valueOf(high));
        }
    } else {
        column = new TextColumn();
    }

    column.setKey(key);

    if (description != null) {
        column.setDescription(description);
    }

    if (format != null) {
        column.setFormat(format);
    }

    if (objective != null) {
        column.setObjective(objective);
    }

    if (fullName != null) {
        column.setFullName(fullName);
    }

    if (goal != null) {
        column.setGoal(goal);
    }

    if (key != null) {
        column.setKey(key);
    }

    if (significantGain != null) {
        column.setSignificantGain(significantGain);
    }

    if (significantLoss != null) {
        column.setSignificantLoss(insignificantLoss);
    }

    if (insignificantLoss != null) {
        column.setInsignificantLoss(insignificantLoss);
    }

    return column;
}

From source file:com.ibm.watson.developer_cloud.util.BooleanToStringTypeAdapter.java

License:Open Source License

@Override
public Boolean read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//w w  w. j  a  va2s  .c  om
        return null;
    } else {
        final String value = in.nextString();

        if (YES.equals(value) || TRUE.equals(value)) {
            return Boolean.TRUE;
        } else if (NO.equals(value) || FALSE.equals(value)) {
            return Boolean.FALSE;
        } else if (value.isEmpty()) {
            return null;
        }

        throw new IllegalArgumentException("Cannot parse JSON value '" + value + "' to Boolean.");
    }
}

From source file:com.ibm.watson.developer_cloud.util.LongToDateTypeAdapter.java

License:Open Source License

@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//from  w ww  .ja v  a2  s.c  o m
        return null;
    } else {
        // nextLong() tries to parse Strings to Longs as well
        return new Date(in.nextLong());
    }
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Converts JSON into a Map representing a {@link QueryAggregation} object.
 *
 * @param in {@link JsonReader} object used for parsing
 * @return Map representing the {@link QueryAggregation} object
 * @throws IOException signals that there has been an IO exception
 *///w  w w  . ja v  a2s.co  m
private HashMap<String, Object> getAggregationMap(JsonReader in) throws IOException {
    HashMap<String, Object> objMap = new HashMap<>();
    while (in.peek() != JsonToken.END_DOCUMENT) {
        parseNext(in, objMap);
    }
    return objMap;
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Checks the next {@link JsonToken} to decide the next appropriate parsing method.
 *
 * @param in {@link JsonReader} object used for parsing
 * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
 * @throws IOException signals that there has been an IO exception
 *///w  w  w.j  av a2  s.  c o m
private void parseNext(JsonReader in, HashMap<String, Object> objMap) throws IOException {
    JsonToken token = in.peek();

    String lastName = "";
    if (token == JsonToken.NAME) {
        lastName = in.nextName();
        token = in.peek();
    }

    switch (token) {
    case BEGIN_ARRAY:
        parseArray(in, objMap, lastName);
        break;
    case BEGIN_OBJECT:
        parseObject(in, objMap, lastName);
        break;
    case STRING:
        objMap.put(lastName, in.nextString());
        break;
    case NUMBER:
        objMap.put(lastName, in.nextDouble());
        break;
    case BOOLEAN:
        objMap.put(lastName, in.nextBoolean());
        break;
    default:
        throw new IOException("Unexpected JSON token encountered");
    }

    collapseMap(objMap);
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Parses a JSON array and adds it to the main object map.
 *
 * @param in {@link JsonReader} object used for parsing
 * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
 * @param name key value to go with the resulting value of this method pass
 * @throws IOException signals that there has been an IO exception
 *///  www  .  ja v a2  s.c  om
private void parseArray(JsonReader in, HashMap<String, Object> objMap, String name) throws IOException {
    List<HashMap<String, Object>> array = new ArrayList<>();
    in.beginArray();

    while (in.peek() != JsonToken.END_ARRAY) {
        HashMap<String, Object> arrayItem = new HashMap<>();
        parseNext(in, arrayItem);
        array.add(arrayItem);
    }

    in.endArray();
    objMap.put(name, array);
}

From source file:com.ibm.watson.discovery.v1.query.AggregationDeserializer.java

License:Open Source License

/**
 * Parses a JSON object and adds it to the main object map.
 *
 * @param in {@link JsonReader} object used for parsing
 * @param objMap Map used to build the structure for the resulting {@link QueryAggregation} object
 * @param name key value to go with the resulting value of this method pass
 * @throws IOException signals that there has been an IO exception
 *//*from   w  w  w.  ja v  a 2  s . co m*/
private void parseObject(JsonReader in, HashMap<String, Object> objMap, String name) throws IOException {
    HashMap<String, Object> innerObject = new HashMap<>();
    in.beginObject();

    while (in.peek() != JsonToken.END_OBJECT) {
        parseNext(in, innerObject);
    }

    in.endObject();
    objMap.put(name, innerObject);
}