Search in sources :

Example 61 with JsonElement

use of com.google.gson.JsonElement in project Avengers by saulmm.

the class GsonDeserializersTest method testThatACollectionDeserialzierDeserializesACollection.

@Test
public void testThatACollectionDeserialzierDeserializesACollection() throws Exception {
    MarvelResultsDeserializer<CollectionItem> marvelResultsDeserializer = new MarvelResultsDeserializer<>();
    JsonElement collectionElement = new JsonParser().parse(getComicsCollectionJsonString());
    Type t = new TypeToken<List<CollectionItem>>() {
    }.getType();
    List<CollectionItem> collectionList = marvelResultsDeserializer.deserialize(collectionElement, t, mock(JsonDeserializationContext.class));
    assertThat(collectionList.isEmpty(), is(false));
    assertNotNull(collectionList.get(0).getThumbnail());
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) JsonElement(com.google.gson.JsonElement) List(java.util.List) CollectionItem(saulmm.avengers.entities.CollectionItem) MarvelResultsDeserializer(saulmm.avengers.rest.utils.deserializers.MarvelResultsDeserializer) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Example 62 with JsonElement

use of com.google.gson.JsonElement in project play-cookbook by spinscale.

the class ApiPlugin method getJson.

private Object getJson(Class clazz, String name) {
    try {
        String data = IOUtils.toString(Request.current().body);
        JsonElement jsonElem = new JsonParser().parse(data);
        if (jsonElem.isJsonObject()) {
            JsonObject json = (JsonObject) jsonElem;
            if (json.has(name)) {
                JsonObject from = json.getAsJsonObject(name);
                Object result = gson.fromJson(from, clazz);
                return result;
            }
        }
    } catch (Exception e) {
        Logger.error("Problem rendering JSON: %s", e.getMessage());
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) JsonParser(com.google.gson.JsonParser)

Example 63 with JsonElement

use of com.google.gson.JsonElement in project play-cookbook by spinscale.

the class ApiPlugin method getJson.

private Object getJson(Class clazz, String name) {
    try {
        String body = Request.current().params.get("body");
        JsonElement jsonElem = new JsonParser().parse(body);
        if (jsonElem.isJsonObject()) {
            JsonObject json = (JsonObject) jsonElem;
            if (json.has(name)) {
                JsonObject from = json.getAsJsonObject(name);
                Object result = gson.fromJson(from, clazz);
                return result;
            }
        }
    } catch (Exception e) {
        Logger.error("Problem rendering JSON: %s", e.getMessage());
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) JsonParser(com.google.gson.JsonParser)

Example 64 with JsonElement

use of com.google.gson.JsonElement in project SimpleNews by liuling07.

the class NewsJsonUtils method readJsonNewsDetailBeans.

public static NewsDetailBean readJsonNewsDetailBeans(String res, String docId) {
    NewsDetailBean newsDetailBean = null;
    try {
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
        JsonElement jsonElement = jsonObj.get(docId);
        if (jsonElement == null) {
            return null;
        }
        newsDetailBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), NewsDetailBean.class);
    } catch (Exception e) {
        LogUtils.e(TAG, "readJsonNewsBeans error", e);
    }
    return newsDetailBean;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) NewsDetailBean(com.lauren.simplenews.beans.NewsDetailBean) JsonParser(com.google.gson.JsonParser)

Example 65 with JsonElement

use of com.google.gson.JsonElement in project SimpleNews by liuling07.

the class WeatherJsonUtils method getCity.

/**
     * 从定位的json字串中获取城市
     * @param json
     * @return
     */
public static String getCity(String json) {
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    JsonElement status = jsonObj.get("status");
    if (status != null && "OK".equals(status.getAsString())) {
        JsonObject result = jsonObj.getAsJsonObject("result");
        if (result != null) {
            JsonObject addressComponent = result.getAsJsonObject("addressComponent");
            if (addressComponent != null) {
                JsonElement cityElement = addressComponent.get("city");
                if (cityElement != null) {
                    return cityElement.getAsString().replace("市", "");
                }
            }
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)398 JsonObject (com.google.gson.JsonObject)221 JsonArray (com.google.gson.JsonArray)121 JsonParser (com.google.gson.JsonParser)88 Map (java.util.Map)55 HashMap (java.util.HashMap)49 JsonPrimitive (com.google.gson.JsonPrimitive)48 Gson (com.google.gson.Gson)47 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)45 IOException (java.io.IOException)40 JsonReader (com.google.gson.stream.JsonReader)30 StringReader (java.io.StringReader)21 Test (org.junit.Test)19 GsonBuilder (com.google.gson.GsonBuilder)18 InputStreamReader (java.io.InputStreamReader)18 Type (java.lang.reflect.Type)17 JsonParseException (com.google.gson.JsonParseException)15 AssetManager (android.content.res.AssetManager)10 List (java.util.List)10