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

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

Introduction

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

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

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  ww w  .j a v  a2s  .  co  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.magnet.android.mms.request.marshall.GsonStreamReader.java

License:Open Source License

public GsonStreamReader(InputStream inputStream) {
    InputStreamReader reader = new InputStreamReader(inputStream);
    jr = new JsonReader(reader);
    jr.setLenient(true);// www. ja v  a  2  s .c om
}

From source file:com.magnet.mmx.sasl.BFOAuthAccessor.java

License:Apache License

public static TokenInfo getTokenInfo(String oauthToken) throws IOException {
    TokenInfo tkInfo = null;//from  w w  w  . j a  va2  s .  c  o m
    HttpURLConnection connection = null;
    InputStream inputStream = null;
    try {
        connection = makeGetRequest(oauthToken);
        int responseCode = connection.getResponseCode();
        if (responseCode == HTTP_STATUS_OK) {
            inputStream = connection.getInputStream();
            Gson gson = new Gson();
            String responseBody = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A").next();
            ;
            LOGGER.debug("Token validation response : {}", responseBody);
            JsonReader reader = new JsonReader(new StringReader(responseBody));
            //JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "utf-8"));
            tkInfo = gson.fromJson(reader, TokenInfo.class);
        } else {
            String message = String.format("Unexpected Response code:%d from endpoint", responseCode);
            LOGGER.warn(message);
        }
        return tkInfo;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
}

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//w  w  w. j  a  va 2  s . c om
 * @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.plugin.models.ExamplesManifest.java

License:Open Source License

public ExamplesManifest(String json) throws IOException {
    this.examples = new HashMap<String, ExampleResource>();

    JsonReader reader = new JsonReader(new StringReader(json));
    reader.beginObject();//from  w ww  . j av  a 2 s  .c o  m
    while (reader.hasNext()) {
        String name = reader.nextName();

        // single example
        reader.beginObject();
        String file = null;
        String description = null;
        while (reader.hasNext()) {
            String key = reader.nextName();
            if (key.equals(FILE_KEY)) {
                file = reader.nextString();
            } else if (key.equals(DESCRIPTION_KEY)) {
                description = reader.nextString();
            }
        }
        reader.endObject();
        examples.put(name, new ExampleResource(name, file, description));
    }

}

From source file:com.meltmedia.cadmium.core.history.HistoryManager.java

License:Apache License

private void readHistoryFile() throws Exception {
    if (contentRoot != null) {
        String path = FileSystemManager.getFileIfCanRead(contentRoot, HISTORY_FILE_NAME);

        if (path != null) {
            Gson gson = new Gson();
            JsonReader reader = null;/*  w  w  w . j  a va2s  . co  m*/
            try {
                reader = new JsonReader(new FileReader(path));
                reader.setLenient(true);
                List<HistoryEntry> entries = gson.fromJson(reader, new TypeToken<List<HistoryEntry>>() {
                }.getType());

                if (entries != null) {
                    history.addAll(entries);
                }
            } finally {
                log.info("Read in {} history entries", history.size());
                if (reader != null) {
                    reader.close();
                }
            }
        }
    }
}

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

/**
 * Cette methode permet de renvoyer une liste de map contenant pour chaque map, les attributs(pass en parametre) et leurs valeurs concernant un objet de la liste(liste d'objet resultant de l'url)
 * @param urlwebservice url du webservice (faire reference  un renvoie
 * d'une liste d'objet)/*from   w  w w . ja  v a 2s  .co m*/
 * @param listeAttribut une liste des attributs a chercher dans le Json
 * @return une liste de Map
 */
public List<Map<String, String>> getListeLectureJsonFromUrl(String urlwebservice, List<String> listeAttribut) {
    try {
        if (!listeAttribut.isEmpty()) {
            String json = MikiWsTools.get(urlwebservice);
            List<Map<String, String>> resultat3 = null;
            final JsonReader reader = new JsonReader(new StringReader(json));
            MikiWsJsonTools classe = new MikiWsJsonTools();
            resultat3 = classe.lectureJson(listeAttribut, reader);
            reader.close();
            return resultat3;
        } 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 une liste de map contenant pour chaque map, les attributs(pass en parametre) et leurs valeurs concernant un objet de la liste( liste d'objet dans le json passe en parametre)
 * @param json les donnes en format json
 * d'une liste d'objet)//from  w  ww  .  ja  v  a  2s  .  c  o  m
 * @param listeAttribut une liste des attributs a chercher dans le Json
 * @return une liste de Map
 */
public List<Map<String, String>> getListeLectureJson(String json, List<String> listeAttribut) {
    try {
        if (!listeAttribut.isEmpty()) {
            List<Map<String, String>> resultat3 = null;
            final JsonReader reader = new JsonReader(new StringReader(json));
            MikiWsJsonTools classe = new MikiWsJsonTools();
            resultat3 = classe.lectureJson(listeAttribut, reader);
            reader.close();
            return resultat3;
        } 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 urlwebservice url du webservice (faire reference  un renvoie d'un
 * seul objet)/*from   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;
    }
}

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  w  w .j ava  2 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;
    }
}