Search in sources :

Example 96 with JsonObject

use of com.google.gson.JsonObject in project Minechem by iopleke.

the class ElementHandler method readFromStream.

private static void readFromStream(InputStream stream) {
    JsonReader jReader = new JsonReader(new InputStreamReader(stream));
    JsonParser parser = new JsonParser();
    Set<Map.Entry<String, JsonElement>> elementsSet = parser.parse(jReader).getAsJsonObject().entrySet();
    int count = 0;
    for (Map.Entry<String, JsonElement> elementEntry : elementsSet) {
        if (!elementEntry.getValue().isJsonObject()) {
            continue;
        }
        JsonObject elementObject = elementEntry.getValue().getAsJsonObject();
        ElementRegistry.getInstance().registerElement(Integer.parseInt(elementEntry.getKey()), elementObject.get("longName").getAsString(), elementObject.get("shortName").getAsString(), elementObject.get("form").getAsString(), elementObject.get("type").getAsString(), Integer.parseInt(elementObject.get("neutrons").getAsString()));
        count++;
    }
    LogHelper.info("Total of " + count + " elements registered");
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Example 97 with JsonObject

use of com.google.gson.JsonObject in project musicbrainz-android by jdamcd.

the class LastFmClient method parseResult.

private LastFmArtist parseResult(InputStream stream) {
    Reader reader = new InputStreamReader(stream);
    JsonObject obj = new JsonParser().parse(reader).getAsJsonObject();
    return new Gson().fromJson(obj.get("artist"), LastFmArtist.class);
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonParser(com.google.gson.JsonParser)

Example 98 with JsonObject

use of com.google.gson.JsonObject in project musicbrainz-android by jdamcd.

the class WikipediaClient method parseResult.

private String parseResult(InputStream stream) {
    Reader reader = new InputStreamReader(stream);
    JsonObject obj = new JsonParser().parse(reader).getAsJsonObject();
    WikipediaBio response = new Gson().fromJson(obj.get("mobileview"), WikipediaBio.class);
    return response.sections.get(0).text;
}
Also used : WikipediaBio(org.musicbrainz.mobile.async.external.result.WikipediaBio) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonParser(com.google.gson.JsonParser)

Example 99 with JsonObject

use of com.google.gson.JsonObject in project JsonPath by jayway.

the class JsonOrgJsonProvider method getMapValue.

@Override
public Object getMapValue(Object obj, String key) {
    try {
        JSONObject jsonObject = toJsonObject(obj);
        Object o = jsonObject.get(key);
        if (!jsonObject.has(key)) {
            return UNDEFINED;
        } else {
            return unwrap(o);
        }
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject)

Example 100 with JsonObject

use of com.google.gson.JsonObject in project JsonPath by jayway.

the class JsonOrgJsonProvider method toIterable.

@Override
public Iterable<?> toIterable(Object obj) {
    try {
        if (isArray(obj)) {
            JSONArray arr = toJsonArray(obj);
            List<Object> values = new ArrayList<Object>(arr.length());
            for (int i = 0; i < arr.length(); i++) {
                values.add(unwrap(arr.get(i)));
            }
            return values;
        } else {
            JSONObject jsonObject = toJsonObject(obj);
            List<Object> values = new ArrayList<Object>();
            for (int i = 0; i < jsonObject.names().length(); i++) {
                String key = (String) jsonObject.names().get(i);
                Object val = jsonObject.get(key);
                values.add(unwrap(val));
            }
            return values;
        }
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject)

Aggregations

JsonObject (com.google.gson.JsonObject)1124 JsonParser (com.google.gson.JsonParser)263 JsonElement (com.google.gson.JsonElement)228 JsonArray (com.google.gson.JsonArray)226 JsonPrimitive (com.google.gson.JsonPrimitive)78 HashMap (java.util.HashMap)78 Test (org.junit.Test)77 Gson (com.google.gson.Gson)70 Map (java.util.Map)66 ArrayList (java.util.ArrayList)63 Test (org.testng.annotations.Test)61 IOException (java.io.IOException)53 InputStreamReader (java.io.InputStreamReader)27 JsonParseException (com.google.gson.JsonParseException)24 File (java.io.File)21 HttpResponse (org.apache.http.HttpResponse)21 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 JsonReader (com.google.gson.stream.JsonReader)17 InputStream (java.io.InputStream)17 URL (java.net.URL)15