List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
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(); }// w ww . j a v a2s.c o m return ""; }
From source file:com.getperka.flatpack.ext.Codex.java
License:Apache License
/** * Reify the given {@link JsonElement} into a Java value. If {@code} element is {@null} or * represents a Json {@code null} value, this method will return {@code null}, otherwise this * method will delegate to {@link #readNotNull(JsonElement, DeserializationContext)}. * /*www .j a v a2s . c o m*/ * @param element the element that contains the value to reify * @param context contextual information for the deserialization process * @return the requested value */ public T read(JsonElement element, DeserializationContext context) { if (element == null || element.isJsonNull()) { return null; } context.pushPath("(" + getClass().getSimpleName() + ".read())"); try { return readNotNull(element, context); } catch (Exception e) { context.fail(e); return null; } finally { context.popPath(); } }
From source file:com.github.easyjsonapi.adapters.EasyJsonApiDeserializer.java
License:Apache License
@Override public JsonApi deserialize(JsonElement jsonElem, Type jsonType, JsonDeserializationContext jsonContext) { JsonApi request = new JsonApi(); if (!jsonElem.isJsonNull()) { boolean existJsonFormat = jsonElem.getAsJsonObject().isJsonNull(); if (!existJsonFormat) { if (Assert.notNull(jsonElem.getAsJsonObject().get("data"))) { request = deserializerData(jsonElem, jsonContext); } else if (Assert.notNull(jsonElem.getAsJsonObject().get("errors"))) { request = deserializerError(jsonElem, jsonContext); }/*from w ww.ja va 2s . c o m*/ } } return request; }
From source file:com.github.GsonPrettyPrinter.java
License:Open Source License
private List<String> toStringList(final JsonElement je) { if (je == null || je.isJsonNull()) return new ArrayList<String>(Arrays.asList(new String[] { "null" })); 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); }//from w ww . j av a 2s .co m throw new RuntimeException("Unsupported Json element: " + je.getClass().getName()); }
From source file:com.google.gwtjsonrpc.server.MapDeserializer.java
License:Apache License
public Map<Object, Object> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Type kt = ((ParameterizedType) typeOfT).getActualTypeArguments()[0]; final Type vt = ((ParameterizedType) typeOfT).getActualTypeArguments()[1]; if (json.isJsonNull()) { return null; }// w w w . ja va 2 s.co m if (kt == String.class) { if (!json.isJsonObject()) { throw new JsonParseException("Expected object for map type"); } final JsonObject p = (JsonObject) json; final Map<Object, Object> r = createInstance(typeOfT); for (final Map.Entry<String, JsonElement> e : p.entrySet()) { final Object v = context.deserialize(e.getValue(), vt); r.put(e.getKey(), v); } return r; } else { if (!json.isJsonArray()) { throw new JsonParseException("Expected array for map type"); } final JsonArray p = (JsonArray) json; final Map<Object, Object> r = createInstance(typeOfT); for (int n = 0; n < p.size();) { final Object k = context.deserialize(p.get(n++), kt); final Object v = context.deserialize(p.get(n++), vt); r.put(k, v); } return r; } }
From source file:com.google.gwtjsonrpc.server.SqlDateDeserializer.java
License:Apache License
public java.sql.Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }// w ww.j a va 2s .com if (!json.isJsonPrimitive()) { throw new JsonParseException("Expected string for date type"); } final JsonPrimitive p = (JsonPrimitive) json; if (!p.isString()) { throw new JsonParseException("Expected string for date type"); } try { return java.sql.Date.valueOf(p.getAsString()); } catch (IllegalArgumentException e) { throw new JsonParseException("Not a date string"); } }
From source file:com.google.gwtjsonrpc.server.SqlTimestampDeserializer.java
License:Apache License
public java.sql.Timestamp deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }// ww w.j a v a 2 s . com if (!json.isJsonPrimitive()) { throw new JsonParseException("Expected string for timestamp type"); } final JsonPrimitive p = (JsonPrimitive) json; if (!p.isString()) { throw new JsonParseException("Expected string for timestamp type"); } return JavaSqlTimestamp_JsonSerializer.parseTimestamp(p.getAsString()); }
From source file:com.google.wave.api.impl.OperationRequestGsonAdaptor.java
License:Apache License
/** * Returns a property of {@code JsonObject} as a {@link String}, then remove * that property./*from w w w . j av a 2 s .c o m*/ * * @param jsonObject the {@code JsonObject} to get the property from. * @param key the key of the property. * @return the property as {@link String}, or {@code null} if not found. */ private static String getPropertyAsStringThenRemove(JsonObject jsonObject, ParamsProperty key) { JsonElement property = jsonObject.get(key.key()); if (property != null) { jsonObject.remove(key.key()); if (property.isJsonNull()) { return null; } return property.getAsString(); } return null; }
From source file:com.google.wave.api.RobotSerializer.java
License:Apache License
/** * Determines the protocol version of a given operation bundle JSON by * inspecting the first operation in the bundle. If it is a * {@code robot.notify} operation, and contains {@code protocolVersion} * parameter, then this method will return the value of that parameter. * Otherwise, this method will return the default version. * * @param operationBundle the operation bundle to check. * @return the wire protocol version of the given operation bundle. *//*from w ww. j av a2 s . co m*/ private ProtocolVersion determineProtocolVersion(JsonArray operationBundle) { if (operationBundle.size() == 0 || !operationBundle.get(0).isJsonObject()) { return defaultProtocolVersion; } JsonObject firstOperation = operationBundle.get(0).getAsJsonObject(); if (!firstOperation.has(RequestProperty.METHOD.key())) { return defaultProtocolVersion; } String method = firstOperation.get(RequestProperty.METHOD.key()).getAsString(); if (isRobotNotifyOperationMethod(method)) { JsonObject params = firstOperation.get(RequestProperty.PARAMS.key()).getAsJsonObject(); if (params.has(ParamsProperty.PROTOCOL_VERSION.key())) { JsonElement protocolVersionElement = params.get(ParamsProperty.PROTOCOL_VERSION.key()); if (!protocolVersionElement.isJsonNull()) { return ProtocolVersion.fromVersionString(protocolVersionElement.getAsString()); } } } return defaultProtocolVersion; }