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.j2.cs.webservices.metrofax.server.json.JsonIntegerTypeAdapter.java

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

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

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

From source file:com.magnet.android.mms.request.JsonUtils.java

License:Open Source License

private static boolean isJson(Reader reader) {
    boolean result = false;
    try {//from w  w  w .ja  va2 s .  c  o  m
        JsonReader jr = new JsonReader(reader);
        jr.setLenient(true);
        JsonToken token = jr.peek();
        result = token.equals(JsonToken.BEGIN_OBJECT) || token.equals(JsonToken.BEGIN_ARRAY);
    } catch (Exception e) {
        Log.w(LOG_TAG, "JsonReader exception:" + e.getMessage());
    }
    return result;
}

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();//w w w .j  av  a 2  s.  c om
    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)// www .  ja v a 2 s .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;
    }
}

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 json les donnes en format json
 * @param listeAttribut une liste des attributs a chercher dans le Json
 * @return un Map/*from w ww. j a v  a2  s  . c o m*/
 */
public Map<String, String> getObjetLectureJson(String json, List<String> listeAttribut) {
    try {
        if (!listeAttribut.isEmpty()) {
            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;
    }
}

From source file:com.nridge.core.io.gson.JSONDocument.java

License:Open Source License

private boolean isNextTokenAnObject(JsonReader aReader) throws IOException {
    JsonToken jsonToken = aReader.peek();

    return (jsonToken == JsonToken.BEGIN_OBJECT);
}

From source file:com.nridge.core.io.gson.JSONDocument.java

License:Open Source License

private void loadDocumentArray(JsonReader aReader, Document aDocument) throws IOException {
    JsonToken jsonToken;/*from  www . ja va2s .  c o  m*/
    Document childDocument;

    do {
        childDocument = new Document(aDocument.getType());
        childDocument.setName(aDocument.getName());
        aDocument.addRelationship(aDocument.getTitle(), childDocument);
        loadDocument(aReader, childDocument);
        jsonToken = aReader.peek();
    } while (jsonToken == JsonToken.BEGIN_OBJECT);
}

From source file:com.nridge.core.io.gson.JSONDocument.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into an internally managed
 * document instance.//from   w w  w  . java 2s. co  m
 *
 * @param aReader Json reader stream instance.
 *
 * @throws IOException I/O related exception.
 */
private void loadDocument(JsonReader aReader, Document aDocument) throws IOException {
    DataBag dataBag;
    JsonToken jsonToken;
    DataField dataField;
    String jsonName, jsonValue, jsonTitle;

    aReader.beginObject();

    jsonToken = aReader.peek();
    while (jsonToken == JsonToken.NAME) {
        jsonName = aReader.nextName();
        jsonTitle = Field.nameToTitle(jsonName);

        jsonToken = aReader.peek();
        switch (jsonToken) {
        case BOOLEAN:
            dataBag = aDocument.getBag();
            dataField = new DataField(jsonName, jsonTitle, aReader.nextBoolean());
            dataBag.add(dataField);
            break;
        case NUMBER:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            if (StringUtils.contains(jsonValue, StrUtl.CHAR_DOT))
                dataField = new DataField(jsonName, jsonTitle, Double.valueOf(jsonValue));
            else
                dataField = new DataField(jsonName, jsonTitle, Long.valueOf(jsonValue));
            dataBag.add(dataField);
            break;
        case STRING:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            Date dateValue = DatUtl.detectCreateDate(jsonValue);
            if (dateValue != null)
                dataField = new DataField(jsonName, jsonTitle, dateValue);
            else
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle, jsonValue);
            dataBag.add(dataField);
            break;
        case NULL:
            dataBag = aDocument.getBag();
            aReader.nextNull();
            dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
            dataBag.add(dataField);
            break;
        case BEGIN_ARRAY:
            aReader.beginArray();
            if (isNextTokenAnObject(aReader)) {
                Document childDocument = new Document(jsonTitle);
                childDocument.setName(jsonName);
                aDocument.addRelationship(jsonTitle, childDocument);
                loadDocumentArray(aReader, childDocument);
            } else {
                dataBag = aDocument.getBag();
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
                jsonToken = aReader.peek();
                while (jsonToken != JsonToken.END_ARRAY) {
                    jsonValue = aReader.nextString();
                    dataField.addValue(jsonValue);
                    jsonToken = aReader.peek();
                }
                dataBag.add(dataField);
            }
            aReader.endArray();
            break;
        case BEGIN_OBJECT:
            Document childDocument = new Document(jsonTitle);
            childDocument.setName(jsonName);
            aDocument.addRelationship(jsonTitle, childDocument);
            loadDocument(aReader, childDocument);
            break;
        default:
            aReader.skipValue();
            break;
        }
        jsonToken = aReader.peek();
    }

    aReader.endObject();
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private boolean isNextTokenAnArray(JsonReader aReader) throws IOException {
    JsonToken jsonToken = aReader.peek();

    return (jsonToken == JsonToken.BEGIN_ARRAY);
}