List of usage examples for com.google.gson JsonObject isJsonNull
public boolean isJsonNull()
From source file:augsburg.se.alltagsguide.common.EventCategory.java
License:Open Source License
@Nullable public static EventCategory fromJson(@NonNull final JsonObject jsonCategory) { if (jsonCategory.isJsonNull()) { return null; }//from ww w . ja v a 2 s . c o m JsonElement idElement = jsonCategory.get("id"); if (idElement.isJsonNull()) { return null; } int id = Helper.getIntOrDefault(idElement, -1); if (id == -1) { return null; } String name = jsonCategory.get("name").getAsString(); int parent = jsonCategory.get("parent").getAsInt(); return new EventCategory(id, name, parent); }
From source file:augsburg.se.alltagsguide.common.EventLocation.java
License:Open Source License
@Nullable public static EventLocation fromJson(@NonNull JsonObject jsonPage) { if (jsonPage.isJsonNull()) { return null; }//from ww w. j a v a2s.co m JsonElement idElement = jsonPage.get("id"); if (idElement.isJsonNull()) { return null; } int id = Helper.getIntOrDefault(idElement, -1); if (id == -1) { return null; } return new EventLocation(id, Helper.getStringOrDefault(jsonPage.get("name"), null), Helper.getStringOrDefault(jsonPage.get("address"), null), Helper.getStringOrDefault(jsonPage.get("town"), null), Helper.getStringOrDefault(jsonPage.get("state"), null), Helper.getIntOrDefault(jsonPage.get("postcode"), 0), Helper.getStringOrDefault(jsonPage.get("region"), null), Helper.getStringOrDefault(jsonPage.get("country"), null), Helper.getFloatOrDefault(jsonPage.get("latitude"), 0.0f), Helper.getFloatOrDefault(jsonPage.get("longitude"), 0.0f)); }
From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java
License:Apache License
private void fillReservedResources(final Map<String, JsonObject> roleMap, final JsonObject reservedResourcesFull) { if (reservedResourcesFull.isJsonNull()) { return;//from w w w .j a v a 2s. c om } for (Map.Entry<String, JsonElement> each : reservedResourcesFull.entrySet()) { for (JsonElement eachResource : each.getValue().getAsJsonArray()) { filledResource(roleMap, eachResource.getAsJsonObject(), "reserved_resources"); } } }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static String getPropertyAsString(JsonObject object, String property) { if (object == null || object.isJsonNull()) { return null; }/*from w ww . ja va 2 s. co m*/ JsonElement element = object.get(property); if (element == null) { return null; } String string; if (!element.isJsonNull() && element.isJsonPrimitive()) { string = element.getAsString(); } else { string = element.toString(); } return string; }
From source file:com.fooock.shodan.model.host.FacetReportDeserializer.java
License:Open Source License
@Override public FacetReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return new FacetReport(0, Collections.emptyList()); }//from w w w .j ava 2 s . c o m JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject == null || jsonObject.isJsonNull()) { return new FacetReport(0, Collections.emptyList()); } JsonElement element = jsonObject.get("matches"); if (element == null || element.isJsonNull()) { // parse directly facets, this is because when searching host with facets // the banners are parsed independently List<Facet> facets = getFacets(jsonObject); return new FacetReport(facets.size(), facets); } else { JsonElement totalElement = jsonObject.get("total"); return parseFacetReport(jsonObject, totalElement); } }
From source file:com.fooock.shodan.model.host.FacetReportDeserializer.java
License:Open Source License
private FacetReport parseFacetReport(JsonObject jsonObject, JsonElement totalElement) { final int total = totalElement.getAsInt(); JsonObject facetsElement = jsonObject.getAsJsonObject("facets"); if (facetsElement == null || facetsElement.isJsonNull()) { return new FacetReport(total, Collections.emptyList()); }//from ww w .j a va 2s .c o m return new FacetReport(total, getFacets(facetsElement)); }
From source file:com.fooock.shodan.model.protocol.ProtocolDeserializer.java
License:Open Source License
@Override public List<Protocol> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final List<Protocol> protocols = new ArrayList<>(); if (json.isJsonNull()) { return protocols; }//from w w w . ja va2 s . com JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.isJsonNull()) { return protocols; } for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); Protocol protocol = new Protocol(key, value.getAsString()); protocols.add(protocol); } return protocols; }
From source file:com.fooock.shodan.model.user.HttpHeaderDeserializer.java
License:Open Source License
@Override public HttpHeader deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return new HttpHeader(Collections.emptyList()); }//from ww w . j a v a2 s. c o m JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.isJsonNull()) { return new HttpHeader(Collections.emptyList()); } final List<Value> values = new ArrayList<>(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); values.add(new Value(key, value.getAsString())); } return new HttpHeader(values); }
From source file:com.googlesource.gerrit.plugins.oauth.BitbucketOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);//w ww . j ava2 s. c o m Response response = request.send(); if (response.getCode() != SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } if (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonObject userObject = jsonObject.getAsJsonObject("user"); if (userObject == null || userObject.isJsonNull()) { throw new IOException("Response doesn't contain 'user' field"); } JsonElement usernameElement = userObject.get("username"); String username = usernameElement.getAsString(); JsonElement displayName = jsonObject.get("display_name"); return new OAuthUserInfo(username, username, null, displayName == null || displayName.isJsonNull() ? null : displayName.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GitLabOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { final String protectedResourceUrl = String.format(PROTECTED_RESOURCE_URL, rootUrl); OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/* w w w . j a v a 2s . c o m*/ Response response = request.send(); if (response.getCode() != SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } JsonObject jsonObject = userJson.getAsJsonObject(); if (jsonObject == null || jsonObject.isJsonNull()) { throw new IOException("Response doesn't contain 'user' field" + jsonObject); } JsonElement id = jsonObject.get("id"); JsonElement username = jsonObject.get("username"); JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); return new OAuthUserInfo(GITLAB_PROVIDER_PREFIX + id.getAsString(), username == null || username.isJsonNull() ? null : username.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), name == null || name.isJsonNull() ? null : name.getAsString(), null); }