List of usage examples for com.google.gson JsonObject isJsonNull
public boolean isJsonNull()
From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java
License:Open Source License
/** * Return is empty meaning null, JSON null or an empty object. */// w ww. ja v a2s.c o m public static boolean jisEmpty(JsonObject object) { return object == null || object.isJsonNull() || object.entrySet().isEmpty(); }
From source file:com.mdlive.sav.MDLiveSearchProvider.java
/** * Successful Response Handler for getting Current Location *//*from ww w .ja v a 2s. co m*/ private void handleFilterSuccessResponse(JSONObject response) { try { hideProgress(); JsonParser parser = new JsonParser(); JsonObject responObj = (JsonObject) parser.parse(response.toString()); if (!responObj.isJsonNull()) { JsonArray responArray = responObj.get("physicians").getAsJsonArray(); if (responArray.size() != 0) { if (responArray.get(0).isJsonObject()) { Log.e("Filter Response", responObj.toString()); Intent intent = new Intent(); intent.putExtra("Response", response.toString()); intent.putExtra("postParams", new Gson().toJson(postParams)); setResult(1, intent); finish(); MdliveUtils.closingActivityAnimation(MDLiveSearchProvider.this); } else { Log.e("Filter Response", responObj.toString()); MdliveUtils.showDialog(MDLiveSearchProvider.this, responArray.getAsString(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.money.manager.ex.investment.prices.FixerService.java
License:Open Source License
private List<SecurityPriceModel> getPricesFromJson(JsonObject root) { ArrayList<SecurityPriceModel> result = new ArrayList<>(); String dateString = root.get("date").getAsString(); JsonObject rates = root.get("rates").getAsJsonObject(); if (rates == null || rates.isJsonNull()) return null; // prices/*from ww w . ja v a2s . c om*/ Set<Map.Entry<String, JsonElement>> entries = rates.entrySet(); for (Map.Entry<String, JsonElement> entry : entries) { SecurityPriceModel priceModel = getSecurityPriceFor(entry, dateString); if (priceModel == null) continue; result.add(priceModel); } return result; }
From source file:com.pinterest.deployservice.rodimus.RodimusManagerImpl.java
License:Apache License
@Override public Long getClusterInstanceLaunchGracePeriod(String clusterName) throws Exception { String url = String.format("%s/v1/groups/%s/config", rodimusUrl, clusterName); String res = httpClient.get(url, null, null, headers, RETRIES); JsonObject jsonObject = gson.fromJson(res, JsonObject.class); if (jsonObject == null || jsonObject.isJsonNull()) { return null; }//w w w.j a v a 2 s .c om JsonPrimitive launchGracePeriod = jsonObject.getAsJsonPrimitive("launchLatencyTh"); if (launchGracePeriod == null || launchGracePeriod.isJsonNull()) { return null; } return launchGracePeriod.getAsLong(); }
From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java
License:Apache License
/** * Calls the CoreServices API with custom JSON writer to write the result to. Uses * streaming APIs to handle large paylods of JSON response and write them to writer. * * @param method GET or POST//from ww w. j ava 2 s . c o m * @param params HTTP Params to be sent * @param timeoutMillis Timeout for connection/socket timeout. Use -1 for the default * timeout. * @param writer Writer to write the JSON result * @throws HttpException when network or other IO issues occur * @throws CallErrorException when server or semantics errors occur */ void call(String method, JsonElement params, String token, int timeoutMillis, JsonWriter writer) throws HttpException, CallErrorException { JsonObject header = new JsonObject(); header.addProperty("user", user); if (token != null) { header.addProperty("token", token); } header.addProperty("client", client); JsonObject request = new JsonObject(); request.addProperty("id", uuidGenerator.generate().toString()); request.addProperty("method", method); request.add("params", params); request.add("header", header); final int effectiveTimeoutMillis = (timeoutMillis > 0) ? timeoutMillis : this.timeoutMillis; log.debug("Calling method " + method + " (user: " + user + ")"); InputStream responseStream = requestFactory.createPostJson(apiUrl, request) .setReadTimeoutMillis(effectiveTimeoutMillis).executeAndStream().getBodyAsStream(); log.debug("Method " + method + " done (user: " + user + ")"); if (responseStream == null) { log.info("Empty response while executing request to " + apiUrl); return; } // We're interested only in `result` and `error` properties of the response JSON JsonObject errorObject = null; try (JsonReader reader = getJsonReader(responseStream)) { if (reader.hasNext()) { if (reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT) { String name = reader.nextName(); if ("error".equals(name)) { if (reader.peek() == JsonToken.BEGIN_OBJECT) { errorObject = gsonBuilder.create().fromJson(reader, JsonObject.class); } else { reader.skipValue(); } } else if ("result".equals(name)) { pushToWriter(reader, writer); } else { reader.skipValue(); } } reader.endObject(); } else { log.warn("Response received is not a JSON object, ignoring. Requsest:" + apiUrl); } } else { log.warn("Received empty response while executing the request to " + apiUrl); } } catch (IOException e) { throw new HttpException("Failed to execute request to '" + apiUrl + "'", e); } if (errorObject != null && !errorObject.isJsonNull()) { throw new CallErrorException("Error: " + errorObject.toString()); } }
From source file:com.slx.funstream.utils.UserSerializer.java
License:Apache License
@Override public CurrentUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonUser = json.getAsJsonObject(); if (jsonUser.isJsonNull()) return null; CurrentUser user = new CurrentUser(); user.setId(jsonUser.get(USER_ID).getAsLong()); user.setName(jsonUser.get(USER_NAME).getAsString()); return user;/*from ww w .j a v a 2 s .co m*/ }
From source file:de.dfki.mmf.input.worldmodel.WorldModelFactory.java
License:Open Source License
private void jsonObjectToLowerCase(JsonObject object) { if (object.isJsonNull()) { return;/*from w w w . j a va 2 s . c om*/ } ArrayList<Map.Entry<String, JsonElement>> modifiedEntryList = new ArrayList<>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { if (entry.getValue().isJsonObject()) { jsonObjectToLowerCase(entry.getValue().getAsJsonObject()); } if (entry.getValue().isJsonArray()) { JsonArray array = entry.getValue().getAsJsonArray(); jsonArrayToLowerCase(array); Map.Entry<String, JsonElement> newEntry = new AbstractMap.SimpleEntry<String, JsonElement>( entry.getKey(), array); modifiedEntryList.add(newEntry); } if (entry.getValue().isJsonPrimitive()) { Gson gson = new Gson(); String entryString = entry.getValue().getAsString().toLowerCase(); String jsonString = gson.toJson(entryString); JsonElement element = gson.fromJson(jsonString, JsonElement.class); Map.Entry<String, JsonElement> newEntry = new AbstractMap.SimpleEntry<String, JsonElement>( entry.getKey(), element); modifiedEntryList.add(newEntry); } } for (Map.Entry<String, JsonElement> newEntry : modifiedEntryList) { object.remove(newEntry.getKey()); object.add(newEntry.getKey().toLowerCase(), newEntry.getValue()); } return; }
From source file:de.sanandrew.mods.turretmod.registry.assembly.TurretAssemblyRecipes.java
License:Creative Commons License
private static boolean processJson(Path file, Ex2Function<JsonObject, Boolean, JsonParseException, IOException> callback) { if (!"json".equals(FilenameUtils.getExtension(file.toString())) || FilenameUtils.getName(file.toString()).startsWith("_")) { return true; }//from ww w.j av a 2 s . co m try (BufferedReader reader = Files.newBufferedReader(file)) { JsonObject json = JsonUtils.fromJson(reader, JsonObject.class); if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); } callback.apply(json); return true; } catch (JsonParseException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Parsing error loading assembly table recipe from %s", file), e); return false; } catch (IOException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Couldn't read recipe from %s", file), e); return false; } }
From source file:de.sanandrew.mods.turretmod.registry.electrolytegen.ElectrolyteRegistry.java
License:Creative Commons License
private static boolean processJson(Path root, Path file) { if (!"json".equals(FilenameUtils.getExtension(file.toString())) || root.relativize(file).toString().startsWith("_")) { return true; }/* w ww . j a v a 2 s . c o m*/ try (BufferedReader reader = Files.newBufferedReader(file)) { JsonObject json = JsonUtils.fromJson(reader, JsonObject.class); if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); } NonNullList<ItemStack> inputItems = JsonUtils.getItemStacks(json.get("electrolytes")); float effectiveness = JsonUtils.getFloatVal(json.get("effectiveness")); int ticksProcessing = JsonUtils.getIntVal(json.get("timeProcessing")); ItemStack trash = ItemStackUtils.getEmpty(); ItemStack treasure = ItemStackUtils.getEmpty(); JsonElement elem = json.get("trash"); if (elem != null && !elem.isJsonNull()) { trash = JsonUtils.getItemStack(elem); } elem = json.get("treasure"); if (elem != null && !elem.isJsonNull()) { treasure = JsonUtils.getItemStack(elem); } registerFuels(inputItems, effectiveness, ticksProcessing, trash, treasure); } catch (JsonParseException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Parsing error loading electrolyte generator recipe from %s", file), e); return false; } catch (IOException e) { TmrConstants.LOG.log(Level.ERROR, String.format("Couldn't read recipe from %s", file), e); return false; } return true; }
From source file:net.felsing.client_cert.utilities.BackgroundProcesses.java
License:Open Source License
private void checkEjbca() { try {/*from w ww.j a v a 2 s . c om*/ EjbcaToolBox ejbcaToolBox = new EjbcaToolBox(properties); JsonObject jsonObject = ejbcaToolBox.getAvailableCas(); ejbcaIsRunning = !jsonObject.isJsonNull(); } catch (Exception e) { ejbcaIsRunning = false; } logger.debug("EJBCA is running: " + Boolean.toString(ejbcaIsRunning)); }