Search in sources :

Example 96 with JsonElement

use of com.google.gson.JsonElement in project LookLook by xinghongfei.

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) {
    }
    return newsDetailBean;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) NewsDetailBean(com.looklook.xinghongfei.looklook.bean.news.NewsDetailBean) JsonParser(com.google.gson.JsonParser)

Example 97 with JsonElement

use of com.google.gson.JsonElement in project LookLook by xinghongfei.

the class NewsJsonUtils method readJsonNewsBeans.

/**
     * 将获取到的json转换为新闻列表对象
     * @param res
     * @param value
     * @return
     */
public static List<NewsBean> readJsonNewsBeans(String res, String value) {
    List<NewsBean> beans = new ArrayList<NewsBean>();
    try {
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
        JsonElement jsonElement = jsonObj.get(value);
        if (jsonElement == null) {
            return null;
        }
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (int i = 1; i < jsonArray.size(); i++) {
            JsonObject jo = jsonArray.get(i).getAsJsonObject();
            if (jo.has("skipType") && "special".equals(jo.get("skipType").getAsString())) {
                continue;
            }
            if (jo.has("TAGS") && !jo.has("TAG")) {
                continue;
            }
            if (!jo.has("imgextra")) {
                NewsBean news = JsonUtils.deserialize(jo, NewsBean.class);
                beans.add(news);
            }
        }
    } catch (Exception e) {
    }
    return beans;
}
Also used : JsonArray(com.google.gson.JsonArray) NewsBean(com.looklook.xinghongfei.looklook.bean.news.NewsBean) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 98 with JsonElement

use of com.google.gson.JsonElement in project RFToolsDimensions by McJty.

the class Filter method parse.

public static Filter parse(JsonElement element) {
    if (element == null) {
        return MATCHALL;
    } else {
        Builder builder = new Builder();
        JsonObject jsonObject = element.getAsJsonObject();
        JSonTools.getElement(jsonObject, "mod").ifPresent(e -> JSonTools.asArrayOrSingle(e).map(JsonElement::getAsString).forEach(builder::mod));
        JSonTools.getElement(jsonObject, "name").ifPresent(e -> JSonTools.asArrayOrSingle(e).map(JsonElement::getAsString).forEach(builder::name));
        JSonTools.getElement(jsonObject, "type").ifPresent(e -> JSonTools.asArrayOrSingle(e).map(el -> DimletType.getTypeByName(el.getAsString())).forEach(builder::type));
        JSonTools.getElement(jsonObject, "feature").ifPresent(e -> JSonTools.asArrayOrSingle(e).map(el -> Feature.getFeatureByName(el.getAsString())).forEach(builder::feature));
        JSonTools.getElement(jsonObject, "meta").ifPresent(e -> JSonTools.asArrayOrSingle(e).map(JsonElement::getAsInt).forEach(builder::meta));
        JSonTools.getElement(jsonObject, "property").ifPresent(e -> JSonTools.asPairs(e).forEach(p -> builder.property(p.getKey(), p.getValue())));
        return builder.build();
    }
}
Also used : JsonElement(com.google.gson.JsonElement) IFilterBuilder(mcjty.rftoolsdim.api.dimlet.IFilterBuilder) JsonObject(com.google.gson.JsonObject) java.util(java.util) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) ByteBuf(io.netty.buffer.ByteBuf) ByteBufTools(mcjty.rftoolsdim.network.ByteBufTools) DimletType(mcjty.rftoolsdim.dimensions.dimlets.types.DimletType) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) JSonTools(mcjty.rftoolsdim.varia.JSonTools) JsonElement(com.google.gson.JsonElement) IFilterBuilder(mcjty.rftoolsdim.api.dimlet.IFilterBuilder) JsonObject(com.google.gson.JsonObject)

Example 99 with JsonElement

use of com.google.gson.JsonElement in project RFToolsDimensions by McJty.

the class Filter method buildElement.

public JsonElement buildElement() {
    if (mods == null && names == null && nameRegexps == null && types == null && features == null && metas == null && properties == null) {
        return null;
    }
    JsonObject jsonObject = new JsonObject();
    JSonTools.addArrayOrSingle(jsonObject, "mod", mods);
    Set<String> namedAndRegexps;
    if (names == null && nameRegexps == null) {
        namedAndRegexps = null;
    } else if (names == null) {
        namedAndRegexps = nameRegexps.stream().map(Pattern::toString).collect(Collectors.toSet());
    } else if (nameRegexps == null) {
        namedAndRegexps = names;
    } else {
        namedAndRegexps = Stream.concat(names.stream(), nameRegexps.stream().map(Pattern::toString)).collect(Collectors.toSet());
    }
    JSonTools.addArrayOrSingle(jsonObject, "name", namedAndRegexps);
    JSonTools.addIntArrayOrSingle(jsonObject, "meta", metas);
    JSonTools.addArrayOrSingle(jsonObject, "type", types == null ? null : types.stream().map(t -> t.dimletType.getName().toLowerCase()).collect(Collectors.toList()));
    JSonTools.addArrayOrSingle(jsonObject, "feature", features == null ? null : features.stream().map(t -> t.name().toLowerCase()).collect(Collectors.toList()));
    JSonTools.addPairs(jsonObject, "property", properties);
    return jsonObject;
}
Also used : JsonElement(com.google.gson.JsonElement) IFilterBuilder(mcjty.rftoolsdim.api.dimlet.IFilterBuilder) JsonObject(com.google.gson.JsonObject) java.util(java.util) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) ByteBuf(io.netty.buffer.ByteBuf) ByteBufTools(mcjty.rftoolsdim.network.ByteBufTools) DimletType(mcjty.rftoolsdim.dimensions.dimlets.types.DimletType) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) JSonTools(mcjty.rftoolsdim.varia.JSonTools) JsonObject(com.google.gson.JsonObject)

Example 100 with JsonElement

use of com.google.gson.JsonElement in project fb-botmill by BotMill.

the class AttachmentDeserializer method deserialize.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
	 * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
	 */
public Attachment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Attachment attachment = delegateGson.fromJson(json, Attachment.class);
    AttachmentType type = attachment.getType();
    Class<? extends Payload> payloadClass = null;
    JsonElement payloadJson = json.getAsJsonObject().get("payload");
    switch(type) {
        case AUDIO:
        case FILE:
        case IMAGE:
        case VIDEO:
            payloadClass = UrlPayload.class;
            break;
        case LOCATION:
            payloadClass = QuickReplyLocationPayload.class;
            break;
        case FALLBACK:
            // nothing.
            break;
        case TEMPLATE:
            // In case of a template I need to check which one to instantiate.
            String payloadTypeString = payloadJson.getAsJsonObject().get("template_type").getAsString();
            PayloadType templateType = PayloadType.valueOf(payloadTypeString.toUpperCase());
            switch(templateType) {
                case AIRLINE_BOARDINGPASS:
                    payloadClass = AirlineBoardingPassTemplatePayload.class;
                    break;
                case AIRLINE_CHECKIN:
                    payloadClass = AirlineCheckinTemplatePayload.class;
                    break;
                case AIRLINE_ITINERARY:
                    payloadClass = AirlineItineraryTemplatePayload.class;
                    break;
                case AIRLINE_UPDATE:
                    payloadClass = AirlineFlightUpdateTemplatePayload.class;
                    break;
                case BUTTON:
                    payloadClass = ButtonTemplatePayload.class;
                    break;
                case GENERIC:
                    payloadClass = GenericTemplatePayload.class;
                    break;
                case LIST:
                    payloadClass = ListTemplatePayload.class;
                    break;
                case RECEIPT:
                    payloadClass = ReceiptTemplatePayload.class;
                    break;
            }
            break;
    }
    Payload payload = context.deserialize(payloadJson, payloadClass);
    attachment.setPayload(payload);
    return attachment;
}
Also used : AttachmentType(co.aurasphere.botmill.fb.model.base.AttachmentType) JsonElement(com.google.gson.JsonElement) Attachment(co.aurasphere.botmill.fb.model.base.Attachment) Payload(co.aurasphere.botmill.fb.model.base.Payload) UrlPayload(co.aurasphere.botmill.fb.model.outcoming.payload.UrlPayload) QuickReplyLocationPayload(co.aurasphere.botmill.fb.model.base.QuickReplyLocationPayload) AirlineCheckinTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineCheckinTemplatePayload) AirlineFlightUpdateTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineFlightUpdateTemplatePayload) GenericTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.GenericTemplatePayload) ReceiptTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ReceiptTemplatePayload) AirlineBoardingPassTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineBoardingPassTemplatePayload) ListTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload) ButtonTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ButtonTemplatePayload) AirlineItineraryTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineItineraryTemplatePayload) PayloadType(co.aurasphere.botmill.fb.model.outcoming.payload.PayloadType)

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