List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
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 ww w.j a v a 2s.c om*/ 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 w w w.j a v a 2s .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.foursquare4j.response.AccessTokenResponse.java
License:Apache License
public AccessTokenResponse(String json) { JsonElement obj = new JsonParser().parse(json).getAsJsonObject().get("access_token"); if (obj == null) { exception = new AccessTokenException(json); accessToken = null;/* w w w . jav a 2s . com*/ } else { accessToken = obj.getAsString(); exception = null; } }
From source file:com.gemapps.saidit.networking.deserializer.TopEntriesDeserializer.java
License:Apache License
private String getBeforeTag(JsonElement data) { JsonElement before = data.getAsJsonObject().get("before"); return !before.isJsonNull() ? before.getAsString() : ""; }
From source file:com.gemapps.saidit.networking.deserializer.TopEntriesDeserializer.java
License:Apache License
private String getAfterTag(JsonElement data) { JsonElement after = data.getAsJsonObject().get("after"); return !after.isJsonNull() ? after.getAsString() : ""; }
From source file:com.gemapps.saidit.networking.deserializer.TopEntriesDeserializer.java
License:Apache License
private String getPictureUrl(JsonElement entryData) { JsonElement imagesPreview = entryData.getAsJsonObject().get("preview"); if (imagesPreview != null && !imagesPreview.isJsonNull()) { JsonElement image = getFirstImage(imagesPreview); JsonElement url = image.getAsJsonObject().get("url"); return url.isJsonNull() ? "" : url.getAsString(); }/*from ww w .j ava2 s . com*/ return ""; }
From source file:com.gemini.domain.dto.deserialize.GeminiNetworkRouterDeserializer.java
@Override public GeminiNetworkRouterDTO deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Gson gson = new GsonBuilder().registerTypeAdapter(GeminiNetworkDTO.class, new GeminiNetworkDeserializer()) .create();/* w ww.jav a 2 s . c o m*/ GeminiNetworkRouterDTO newRouter = new GeminiNetworkRouterDTO(); //first the name try { newRouter.setName(json.getAsJsonObject().get("name").getAsString()); } catch (NullPointerException | JsonSyntaxException | IllegalStateException ex) { Logger.error("Malformed JSON: no name specified for Network router"); } try { newRouter.setCloudID(json.getAsJsonObject().get("cloudID").getAsString()); } catch (NullPointerException | JsonSyntaxException | IllegalStateException ex) { Logger.debug("no cloud id for router {}", newRouter.getName()); } //now the gateway try { newRouter.setGateway(gson.fromJson(json.getAsJsonObject().get("gateway"), GeminiNetworkDTO.class)); } catch (NullPointerException | JsonSyntaxException | IllegalStateException ex) { Logger.debug("Malformed JSON: No gateway specified for Network Router {}", newRouter.getName()); } //now the routes, no straight forward way to convert to the HashMap try { JsonArray routeArray = json.getAsJsonObject().get("routes").getAsJsonArray(); for (JsonElement e : routeArray) { //format is nextHop, dest String strRoute = e.getAsString(); List<String> splitRoutes = Splitter.on(',').splitToList(strRoute); if (splitRoutes.size() != 2) { Logger.error("Malformed JSON: Invalid route, does not have two map entries. Router {}", newRouter.getName()); } else { newRouter.addRouter(splitRoutes.get(0), splitRoutes.get(1)); } } } catch (NullPointerException npe) { Logger.debug("No routes provided for {}", newRouter.getName()); } //the interfaces will be handled at the environment level because they //need to added by reference instead of new objects return newRouter; }
From source file:com.getperka.flatpack.codexes.DateTimeZoneCodex.java
License:Apache License
@Override public DateTimeZone readNotNull(JsonElement element, DeserializationContext context) { return DateTimeZone.forID(element.getAsString()); }
From source file:com.getperka.flatpack.codexes.EntityCodex.java
License:Apache License
/** * Performs a minimal amount of work to create an empty stub object to fill in later. * /*from w w w .j a va 2 s.c o m*/ * @param element a JsonObject containing a {@code uuid} property. If {@code null}, a * randomly-generated UUID will be assigned to the allocated object * @param context this method will call {@link DeserializationContext#putEntity} to store the * newly-allocated entity */ public T allocate(JsonElement element, DeserializationContext context) { JsonElement uuidElement = element.getAsJsonObject().get("uuid"); if (uuidElement == null) { context.fail(new IllegalArgumentException("Data entry missing uuid:\n" + element.toString())); } UUID uuid = UUID.fromString(uuidElement.getAsString()); T toReturn = allocate(uuid, context, true); // Register PostUnpack methods if (!postUnpackMethods.isEmpty()) { context.addPostWork(new PostUnpackInvoker(toReturn, postUnpackMethods)); } return toReturn; }
From source file:com.getperka.flatpack.codexes.EntityCodex.java
License:Apache License
@Override public T readNotNull(JsonElement element, DeserializationContext context) { UUID uuid = UUID.fromString(element.getAsString()); HasUuid entity = context.getEntity(uuid); /*/* ww w .ja va 2 s. c o m*/ * If the UUID is a reference to an entity that isn't in the data section, delegate to the * allocate() method. The entity will either be provided by an EntityResolver or a blank entity * will be created if possible. */ if (entity == null) { entity = allocate(uuid, context, true); } try { return clazz.cast(entity); } catch (ClassCastException e) { throw new ClassCastException("Cannot cast a " + entity.getClass().getName() + " to a " + clazz.getName() + ". Duplicate UUID in data payload?"); } }