List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:com.tsc9526.monalisa.tools.json.MelpJson.java
License:Open Source License
private static Object toObject(JsonElement e) { if (e == null || e.isJsonNull()) { return null; } else if (e.isJsonPrimitive()) { return primitive((JsonPrimitive) e); } else if (e.isJsonObject()) { return parseToDataMap(e.getAsJsonObject()); } else if (e.isJsonArray()) { List<Object> list = new ArrayList<Object>(); JsonArray array = (JsonArray) e; for (int i = 0; i < array.size(); i++) { JsonElement je = array.get(i); Object v = toObject(je); list.add(v);/*from w ww . ja v a 2s . co m*/ } return list; } else { return e; } }
From source file:com.uprizer.sensearray.freetools.json.GsonPrettyPrinter.java
License:Open Source License
private List<String> toStringList(final JsonElement je) { if (je.isJsonPrimitive()) return Collections.singletonList(je.getAsJsonPrimitive().toString()); if (je.isJsonArray()) { final JsonArray jsonArray = je.getAsJsonArray(); return arrayToStringList(jsonArray); } else if (je.isJsonObject()) { final JsonObject jsonObject = je.getAsJsonObject(); return objectToStringList(jsonObject); } else if (je.isJsonNull()) { return Collections.singletonList("null"); } else {/*from w ww. jav a2s .com*/ throw new RuntimeException("Unsupported Json element: " + je.getClass().getName()); } }
From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java
License:Apache License
@Override public JsonElement requestJson(String path, String requestBody, HttpVerb verb) throws RestApiException { try {/* w w w . j a v a 2 s.co m*/ HttpResponse response = requestRest(path, requestBody, verb); HttpEntity entity = response.getEntity(); if (entity == null) { return null; } checkContentType(entity); JsonElement ret = parseResponse(entity.getContent()); if (ret.isJsonNull()) { throw new RestApiException("Unexpectedly empty response."); } return ret; } catch (IOException e) { throw new RestApiException("Request failed.", e); } }
From source file:com.vmware.admiral.closures.services.adapter.AdmiralAdapterService.java
License:Open Source License
private LogConfig prepareLogConfig(ContainerConfiguration configuration) { JsonElement jsonLogConfig = configuration.logConfiguration; LogConfig logConfig = new LogConfig(); if (jsonLogConfig == null || !jsonLogConfig.isJsonObject()) { // set default log configuration logConfig.type = "json-file"; logConfig.config = new HashMap<>(); logConfig.config.put("max-size", MAX_LOG_FILE_SIZE); return logConfig; }//ww w.j a va2 s . c o m JsonObject jsonObject = (JsonObject) jsonLogConfig; JsonElement typeElement = jsonObject.get("type"); if (typeElement == null || typeElement.isJsonNull()) { logConfig.type = "json-file"; } else { logConfig.type = typeElement.getAsString(); } Map<String, String> configMap = new HashMap<>(); JsonElement configElement = jsonObject.get("config"); if (configElement == null || configElement.isJsonNull()) { logConfig.config = configMap; return logConfig; } JsonObject jsonConfig = configElement.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonConfig.entrySet()) { configMap.put(entry.getKey(), entry.getValue().getAsString()); } logConfig.config = configMap; return logConfig; }
From source file:com.vmware.admiral.compute.container.maintenance.ContainerStatsEvaluator.java
License:Open Source License
private static void calculateCpuUsage(ContainerStats state, Map<String, JsonElement> stats) { try {//from w w w. j a v a 2 s . c o m JsonElement cpu_stats_json = stats.get("cpu_stats"); if (cpu_stats_json == null || cpu_stats_json.isJsonNull()) { Utils.logWarning("cpu_stats is null."); return; } JsonObject cpu_stats = cpu_stats_json.getAsJsonObject(); JsonElement systemCpuUsageValue = cpu_stats.get("system_cpu_usage"); if (systemCpuUsageValue == null || systemCpuUsageValue.isJsonNull()) { Utils.logWarning("system_cpu_usage is null."); return; } long system_cpu_usage = systemCpuUsageValue.getAsLong(); JsonElement cpu_usage_json = cpu_stats.get("cpu_usage"); if (cpu_usage_json == null || cpu_usage_json.isJsonNull()) { Utils.logWarning("cpu_usage is null."); return; } JsonObject cpu_usage = cpu_usage_json.getAsJsonObject(); JsonElement totalUsageValue = cpu_usage.get("total_usage"); if (totalUsageValue == null || totalUsageValue.isJsonNull()) { Utils.logWarning("totalUsageValue is null."); return; } long total_usage = totalUsageValue.getAsLong(); JsonElement percpu_usage_json = cpu_usage.get("percpu_usage"); if (percpu_usage_json == null || percpu_usage_json.isJsonNull()) { Utils.logWarning("percpu_usage is null."); return; } JsonArray percpu_usage = percpu_usage_json.getAsJsonArray(); JsonElement precpu_stats_json = stats.get("precpu_stats"); if (precpu_stats_json == null || precpu_stats_json.isJsonNull()) { Utils.logWarning("precpu_stats is null."); return; } JsonObject precpu_stats = precpu_stats_json.getAsJsonObject(); JsonElement system_cpu_usage_json = precpu_stats.get("system_cpu_usage"); if (system_cpu_usage_json == null || system_cpu_usage_json.isJsonNull()) { Utils.logWarning("system_cpu_usage is null."); return; } long presystem_cpu_usage = system_cpu_usage_json.getAsLong(); JsonElement precpu_usage_json = precpu_stats.get("cpu_usage"); if (precpu_usage_json == null || precpu_usage_json.isJsonNull()) { Utils.logWarning("precpu_usage is null."); return; } JsonObject precpu_usage = precpu_usage_json.getAsJsonObject(); JsonElement pretotal_usage_json = precpu_usage.get("total_usage"); if (pretotal_usage_json == null || pretotal_usage_json.isJsonNull()) { Utils.logWarning("total_usage is null."); return; } long pretotal_usage = pretotal_usage_json.getAsLong(); long cpuDelta = total_usage - pretotal_usage; long systemDelta = system_cpu_usage - presystem_cpu_usage; if (systemDelta > 0 && cpuDelta > 0) { double cpuUsage = (((double) cpuDelta / systemDelta) * percpu_usage.size()) * 100.0; state.cpuUsage = Math.round(cpuUsage * 100d) / 100d; } } catch (Exception e) { Utils.logWarning("Error during container stats CPU usage calculations: %s", Utils.toString(e)); } }
From source file:com.vmware.admiral.compute.container.maintenance.ContainerStatsEvaluator.java
License:Open Source License
private static void setContainerStopped(ContainerStats state, Map<String, JsonElement> stats) { try {/*from www .j a v a 2s . co m*/ JsonElement read_json = stats.get("read"); if (read_json == null || read_json.isJsonNull()) { Utils.logWarning("read is null."); return; } String read = read_json.getAsString(); state.containerStopped = Boolean.FALSE; if (CONTAINER_STOPPED_TIME.equals(read)) { state.containerStopped = Boolean.TRUE; } } catch (Exception e) { Utils.logWarning("Error during container stats status calculations: %s", Utils.toString(e)); } }
From source file:com.vmware.dcp.common.serialization.ObjectMapTypeConverter.java
License:Open Source License
@Override public Map<String, Object> deserialize(JsonElement json, Type unused, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("The json element is not valid"); }/* w ww. ja va 2 s .c om*/ Map<String, Object> result = new HashMap<String, Object>(); JsonObject jsonObject = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement element = entry.getValue(); if (element.isJsonObject()) { result.put(key, element.toString()); } else if (element.isJsonNull()) { result.put(key, null); } else if (element.isJsonPrimitive()) { result.put(key, element.getAsString()); } else { throw new JsonParseException("The json element is not valid for key:" + key + " value:" + element); } } return result; }
From source file:com.vmware.xenon.common.serialization.ObjectCollectionTypeConverter.java
License:Open Source License
@Override public Collection<Object> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { throw new JsonParseException("Expecting a json array object but found: " + json); }//w ww .j ava2 s.co m Collection<Object> result; if (TYPE_SET.equals(type)) { result = new HashSet<>(); } else if (TYPE_LIST.equals(type) || TYPE_COLLECTION.equals(type)) { result = new LinkedList<>(); } else { throw new JsonParseException("Unexpected target type: " + type); } JsonArray jsonArray = json.getAsJsonArray(); for (JsonElement entry : jsonArray) { if (entry.isJsonNull()) { result.add(null); } else if (entry.isJsonPrimitive()) { JsonPrimitive elem = entry.getAsJsonPrimitive(); Object value = null; if (elem.isBoolean()) { value = elem.getAsBoolean(); } else if (elem.isString()) { value = elem.getAsString(); } else if (elem.isNumber()) { // We don't know if this is an integer, long, float or double... BigDecimal num = elem.getAsBigDecimal(); try { value = num.longValueExact(); } catch (ArithmeticException e) { value = num.doubleValue(); } } else { throw new RuntimeException("Unexpected value type for json element:" + elem); } result.add(value); } else { // keep JsonElement to prevent stringified json issues result.add(entry); } } return result; }
From source file:com.wallellen.wechat.common.util.json.GsonHelper.java
License:Open Source License
public static boolean isNull(JsonElement element) { return element == null || element.isJsonNull(); }
From source file:com.wallellen.wechat.mp.util.json.WxMpMassNewsArticleGsonAdapter.java
License:Open Source License
public WxMpMassNews.WxMpMassNewsArticle deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject articleInfo = jsonElement.getAsJsonObject(); WxMpMassNews.WxMpMassNewsArticle article = new WxMpMassNews.WxMpMassNewsArticle(); JsonElement title = articleInfo.get("title"); if (title != null && !title.isJsonNull()) { article.setTitle(GsonHelper.getAsString(title)); }// w ww .j a v a2 s. c o m JsonElement content = articleInfo.get("content"); if (content != null && !content.isJsonNull()) { article.setContent(GsonHelper.getAsString(content)); } JsonElement contentSourceUrl = articleInfo.get("content_source_url"); if (contentSourceUrl != null && !contentSourceUrl.isJsonNull()) { article.setContentSourceUrl(GsonHelper.getAsString(contentSourceUrl)); } JsonElement author = articleInfo.get("author"); if (author != null && !author.isJsonNull()) { article.setAuthor(GsonHelper.getAsString(author)); } JsonElement digest = articleInfo.get("digest"); if (digest != null && !digest.isJsonNull()) { article.setDigest(GsonHelper.getAsString(digest)); } JsonElement thumbMediaId = articleInfo.get("thumb_media_id"); if (thumbMediaId != null && !thumbMediaId.isJsonNull()) { article.setThumbMediaId(GsonHelper.getAsString(thumbMediaId)); } JsonElement showCoverPic = articleInfo.get("show_cover_pic"); if (showCoverPic != null && !showCoverPic.isJsonNull()) { article.setShowCoverPic(GsonHelper.getAsBoolean(showCoverPic)); } return article; }