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

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

Introduction

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

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

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();
        return null;
    }/*from  w  w  w .  ja  va  2 s.  c  om*/
    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();
        return null;
    }//  w  ww.  jav  a 2  s.  c  om

    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();
        return null;
    }/*w  w w. j  ava  2 s  . c  o m*/

    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();
        return null;
    }/*from  w ww  .  j  av a  2s  . c  o  m*/

    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();
        return null;
    } else {/*from   w w w  . j a v  a2s.  co  m*/
        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();
        return null;
    } else {/*from  w ww.  jav a 2s. c  om*/
        // nextLong() tries to parse Strings to Longs as well
        return new Date(in.nextLong());
    }
}

From source file:com.j2.cs.webservices.metrofax.server.json.JsonIntegerTypeAdapter.java

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

    try {
        String result = in.nextString();
        if (StringUtils.isBlank(result)) {
            return null;
        }
        return Integer.parseInt(result);
    } catch (NumberFormatException e) {
        return null;
    }
}

From source file:com.j2.cs.webservices.metrofax.server.json.JsonLongTypeAdapter.java

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

    try {
        String result = in.nextString();
        if (StringUtils.isBlank(result)) {
            return null;
        }
        return Long.parseLong(result);
    } catch (NumberFormatException e) {
        return null;
    }
}

From source file:com.miki.webapp.webservicerestful.MikiWsJsonTools.java

public Map<String, String> lecture(List<String> listeAttribut, final JsonReader reader) throws IOException {
    Map<String, String> resultat = new HashMap<>();
    reader.beginObject();/*from w w w .j a v a  2s  .  c  o m*/
    while (reader.hasNext()) {
        final String name = reader.nextName();
        int iter = 0;
        for (String attribut : listeAttribut) {
            if (name.equals(attribut)) {
                if (reader.peek() == JsonToken.NULL) {
                    reader.nextNull();
                    resultat.put(attribut, null);
                } else {
                    resultat.put(attribut, reader.nextString());
                }
            } else {
                iter++;
            }
        }

        if (iter == listeAttribut.size()) {
            reader.skipValue();
        }
    }
    reader.endObject();
    return resultat;
}

From source file:com.miki.webapp.webservicerestful.MikiWsJsonTools.java

/**
 * Cette methode permet de renvoyer un map contenant les attributs(pass en parametre) et leurs valeurs
 * @param urlwebservice url du webservice (faire reference  un renvoie d'un
 * seul objet)/*w  ww  . ja v a  2s .c  o m*/
 * @param listeAttribut une liste des attributs a chercher dans le Json
 * @return un Map
 */
public Map<String, String> getObjetLectureJsonFromUrl(String urlwebservice, List<String> listeAttribut) {
    try {
        if (!listeAttribut.isEmpty()) {
            String json = MikiWsTools.get(urlwebservice);
            final JsonReader reader = new JsonReader(new StringReader(json));
            Map<String, String> resultat = new HashMap<>();
            reader.beginObject();
            while (reader.hasNext()) {
                final String name = reader.nextName();
                int iter = 0;
                for (String attribut : listeAttribut) {
                    if (name.equals(attribut)) {
                        if (reader.peek() == JsonToken.NULL) {
                            reader.nextNull();
                            resultat.put(attribut, null);
                        } else {
                            resultat.put(attribut, reader.nextString());
                        }
                    } else {
                        iter++;
                    }
                }
                if (iter == listeAttribut.size()) {
                    reader.skipValue();
                }
            }
            reader.endObject();
            return resultat;
        } else {
            return null;
        }

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Echec de l'opration");
        return null;
    }
}