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

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

Introduction

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

Prototype

public void skipValue() throws IOException 

Source Link

Document

Skips the next value recursively.

Usage

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();/*w  ww .j a v  a 2  s . 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 w  w.ja v a2 s  . c om
        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();/*  ww w .  j  av  a  2  s  .  c o  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 w  w .  j a v  a2s.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.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();/*from   w ww .  j  a va  2s  .co  m*/
        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.ichi2.anki.AnkiDroidProxy.java

License:Open Source License

/**
 * Parses a JSON InputStream to a list of SharedDecks efficiently 
 * @param is InputStream to parse and convert. It only works with the reply from
 *        getSharedDecks request/* w ww  .  j a v a2s. c om*/
 * @return List of SharedDecks that were parsed
 * @throws Exception 
 */
public static List<SharedDeck> parseGetSharedDecksResponce(InputStream is) throws Exception {
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 409600);
    JsonReader reader = new JsonReader(rd);
    List<SharedDeck> sharedDecks = new ArrayList<SharedDeck>();
    try {
        reader.beginArray();
        int count = 0;
        while (reader.hasNext()) {
            SharedDeck sd = new SharedDeck();
            reader.beginArray();
            sd.setId(reader.nextInt()); // SD_ID
            reader.skipValue(); // SD_USERNAME
            sd.setTitle(reader.nextString()); // SD_TITLE
            reader.skipValue(); // SD_DESCRIPTION
            reader.skipValue(); // SD_TAGS
            reader.skipValue(); // SD_VERSION
            sd.setFacts(reader.nextInt()); // SD_FACTS
            sd.setSize(reader.nextInt()); // SD_SIZE
            reader.skipValue(); // SD_COUNT
            reader.skipValue(); // SD_MODIFIED
            reader.skipValue(); // SD_FNAME
            reader.endArray();
            sharedDecks.add(sd);
            count++;
        }
        reader.endArray();
        reader.close();
        Log.d(AnkiDroidApp.TAG, "parseGetSharedDecksResponce: found " + count + " shared decks");
    } catch (Exception e) {
        Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
        sharedDecks.clear();
        throw e;
    }

    return sharedDecks;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.apns.APNSPayloadInfo.java

License:Apache License

/**
 * Parse payload json string to extract specific properties from the APNS Payload json.
 *
 * @param payloadJSON/*from ww  w  .java 2 s  . com*/
 * @return
 */
public static APNSPayloadInfo parse(String payloadJSON) {
    JsonReader reader = new JsonReader(new StringReader(payloadJSON));
    try {
        APNSPayloadInfo rv = null;
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equalsIgnoreCase(Constants.PAYLOAD_MMX_KEY)) {
                rv = readMMXObject(reader);
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return rv;
    } catch (Throwable t) {
        LOGGER.warn("Exception in parsing payloadJSON:{}", payloadJSON, t);
        return null;
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.apns.APNSPayloadInfo.java

License:Apache License

private static APNSPayloadInfo readMMXObject(JsonReader reader) throws IOException {
    APNSPayloadInfo info = new APNSPayloadInfo();
    reader.beginObject();/*  w  w  w .  j a v a  2  s .c  o m*/
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(Constants.PAYLOAD_ID_KEY)) {
            info.messageId = reader.nextString();
        } else if (name.equals(Constants.PAYLOAD_TYPE_KEY)) {
            info.type = reader.nextString();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return info;
}

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 .ja  v a  2  s .  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)//from w  w w. ja v  a 2s  . co  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;
    }
}