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;
}
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;
}
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();
}
}
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;
}
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;
}
Aggregations