List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:angularBeans.remote.InvocationHandler.java
License:LGPL
private void update(Object o, JsonObject params) { if (params != null) { // boolean firstIn = false; for (Map.Entry<String, JsonElement> entry : params.entrySet()) { JsonElement value = entry.getValue(); String name = entry.getKey(); if ((name.equals("sessionUID")) || (name.equals("args"))) { continue; }/*from w ww .ja v a 2 s . co m*/ if ((value.isJsonObject()) && (!value.isJsonNull())) { String getName; try { getName = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method getter = o.getClass().getMethod(getName); Object subObj = getter.invoke(o); // logger.log(Level.INFO, "#entring sub object "+name); update(subObj, value.getAsJsonObject()); } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } } // ------------------------------------ if (value.isJsonArray()) { try { String getter = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method get = o.getClass().getDeclaredMethod(getter); Type type = get.getGenericReturnType(); ParameterizedType pt = (ParameterizedType) type; Type actType = pt.getActualTypeArguments()[0]; String className = actType.toString(); className = className.substring(className.indexOf("class") + 6); Class clazz = Class.forName(className); JsonArray array = value.getAsJsonArray(); Collection collection = (Collection) get.invoke(o); Object elem; for (JsonElement element : array) { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); elem = element; if (primitive.isBoolean()) elem = primitive.getAsBoolean(); if (primitive.isString()) { elem = primitive.getAsString(); } if (primitive.isNumber()) elem = primitive.isNumber(); } else { elem = util.deserialise(clazz, element); } try { if (collection instanceof List) { if (collection.contains(elem)) collection.remove(elem); } collection.add(elem); } catch (UnsupportedOperationException e) { Logger.getLogger("AngularBeans").log(java.util.logging.Level.WARNING, "trying to modify an immutable collection : " + name); } } } catch (Exception e) { e.printStackTrace(); } } // ------------------------------------------ if (value.isJsonPrimitive() && (!name.equals("setSessionUID"))) { try { if (!CommonUtils.hasSetter(o.getClass(), name)) { continue; } name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Class type = null; for (Method set : o.getClass().getDeclaredMethods()) { if (CommonUtils.isSetter(set)) { if (set.getName().equals(name)) { Class<?>[] pType = set.getParameterTypes(); type = pType[0]; break; } } } if (type.equals(LobWrapper.class)) continue; Object param = null; if ((params.entrySet().size() >= 1) && (type != null)) { param = CommonUtils.convertFromString(value.getAsString(), type); } o.getClass().getMethod(name, type).invoke(o, param); } catch (Exception e) { e.printStackTrace(); } } } } }
From source file:at.orz.arangodb.util.JsonUtils.java
License:Apache License
public static double toDouble(JsonElement elem) { if (elem != null && !elem.isJsonNull()) { JsonPrimitive primitive = elem.getAsJsonPrimitive(); if (primitive.isNumber()) { return primitive.getAsDouble(); } else if (primitive.isString()) { if ("INF".equals(primitive.getAsString())) { return Double.POSITIVE_INFINITY; } else if ("NaN".equals(primitive.getAsString())) { return Double.NaN; }//from www . j av a 2 s. co m } } return Double.NaN; }
From source file:au.edu.ausstage.tweetgatherer.MessageProcessor.java
License:Open Source License
/** * A private method to sanitise the message * * @param jsonTweetObject the original message object * @param idHash the hash of the message id * * @return the sanitised message object *//*from w ww .j av a2 s .c o m*/ private JsonObject sanitiseMessage(JsonObject jsonTweetObject, String idHash) { // remove the id and replace it with the hash jsonTweetObject.remove("id"); jsonTweetObject.addProperty("id", idHash); // replace the "in_reply_to_user_id" field with a hash if present if (jsonTweetObject.has("in_reply_to_user_id") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("in_reply_to_user_id"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("in_reply_to_user_id"); jsonTweetObject.addProperty("in_reply_to_user_id", hash); } } // replace the "in_reply_to_status_id" field with a hash if present if (jsonTweetObject.has("in_reply_to_status_id") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("in_reply_to_status_id"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("in_reply_to_status_id"); jsonTweetObject.addProperty("in_reply_to_status_id", hash); } } // replace the "retweeted_status" field with a hash if present if (jsonTweetObject.has("retweeted_status") == true) { // get a hash of the value JsonElement elem = jsonTweetObject.get("retweeted_status"); if (elem.isJsonNull() == false) { String hash = HashUtils.hashValue(elem.getAsString()); // update the value jsonTweetObject.remove("retweeted_status"); jsonTweetObject.addProperty("retweeted_status", hash); } } // return the sanitised object return jsonTweetObject; }
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; }// w ww .j a v a2 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 www . j a v a 2s. c o 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:augsburg.se.alltagsguide.common.EventPage.java
License:Open Source License
@NonNull public static EventPage fromJson(@NonNull final JsonObject jsonPage) { Page page = Page.fromJson(jsonPage); //TODO jsonPage.get("page") !? Event event = Event.fromJson(jsonPage.get("event").getAsJsonObject(), page.getId()); JsonElement locationElement = jsonPage.get("location"); EventLocation location = null;//ww w . j ava 2 s. c om if (locationElement != null && !locationElement.isJsonNull()) { location = EventLocation.fromJson(locationElement.getAsJsonObject()); } List<EventTag> tags = EventTag.fromJson(jsonPage.get("tags").getAsJsonArray()); List<EventCategory> categories = EventCategory.fromJson(jsonPage.get("categories").getAsJsonArray()); return new EventPage(page, event, location, tags, categories); }
From source file:augsburg.se.alltagsguide.common.Location.java
License:Open Source License
@NonNull public static Location fromJson(JsonObject jsonPage) { int id = jsonPage.get("id").getAsInt(); String name = jsonPage.get("name").getAsString(); String icon = jsonPage.get("icon").isJsonNull() ? null : jsonPage.get("icon").getAsString(); String path = jsonPage.get("path").getAsString(); String description = jsonPage.get("description").getAsString(); boolean global = false; if (jsonPage.has("global")) { global = !jsonPage.get("global").isJsonNull() && jsonPage.get("global").getAsBoolean(); }/*ww w. j a v a 2s .c o m*/ int color = jsonPage.get("color").isJsonNull() ? Color.parseColor("#00BCD4") : Color.parseColor(jsonPage.get("color").getAsString()); String cityImage = jsonPage.get("cover_image").isJsonNull() ? "" : jsonPage.get("cover_image").getAsString(); if (cityImage == null || "".equals(cityImage)) { cityImage = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Brandenburger_Tor_abends.jpg/300px-Brandenburger_Tor_abends.jpg"; } float latitude = 0.0f; //TODO float longitude = 0.0f; //TODO boolean debug = false; if (jsonPage.has("live")) { JsonElement elem = jsonPage.get("live"); if (elem != null && !elem.isJsonNull()) { debug = elem.getAsBoolean(); } } return new Location(id, name, icon, path, description, global, color, cityImage, latitude, longitude, debug); }
From source file:augsburg.se.alltagsguide.common.Page.java
License:Open Source License
@NonNull public static Page fromJson(@NonNull final JsonObject jsonPage) { int id = jsonPage.get("id").getAsInt(); String title = jsonPage.get("title").getAsString(); String type = jsonPage.get("type").getAsString(); String status = jsonPage.get("status").getAsString(); long modified; try {//from w w w. j a va2s . c om modified = Helper.FROM_DATE_FORMAT.parse(jsonPage.get("modified_gmt").getAsString()).getTime(); } catch (ParseException e) { Ln.e(e); modified = -1; } String description = jsonPage.get("excerpt").getAsString(); String content = jsonPage.get("content").getAsString(); int parentId = jsonPage.get("parent").getAsInt(); int order = jsonPage.get("order").getAsInt(); String thumbnail = jsonPage.get("thumbnail").isJsonNull() ? "" : jsonPage.get("thumbnail").getAsString(); Author author = Author.fromJson(jsonPage.get("author").getAsJsonObject()); List<AvailableLanguage> languages = AvailableLanguage.fromJson(jsonPage.get("available_languages")); boolean autoTranslated = false; if (jsonPage.has("automatic_translation")) { JsonElement elem = jsonPage.get("automatic_translation"); if (elem != null && !elem.isJsonNull()) { autoTranslated = elem.getAsBoolean(); } } return new Page(id, title, type, status, modified, description, content, parentId, order, thumbnail, author, autoTranslated, languages); }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }/*from w w w . j a v a2 s .com*/ Object o = peekStack(); if (o instanceof Iterator) { Object secondToTop = stack.get(stack.size() - 2); boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject(); Iterator<?> iterator = (Iterator<?>) o; if (iterator.hasNext()) { if (isObject) { return JsonToken.NAME; } else { stack.add(iterator.next()); return peek(); } } else { return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY; } } else if (o instanceof JsonElement) { JsonElement el = (JsonElement) o; if (el.isJsonObject()) { return JsonToken.BEGIN_OBJECT; } else if (el.isJsonArray()) { return JsonToken.BEGIN_ARRAY; } else if (el.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) o; if (primitive.isString()) { return JsonToken.STRING; } else if (primitive.isBoolean()) { return JsonToken.BOOLEAN; } else if (primitive.isNumber()) { return JsonToken.NUMBER; } else { throw new AssertionError(); } } else if (el.isJsonNull()) { return JsonToken.NULL; } throw new AssertionError(); } else if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }
From source file:bind.JsonTreeWriter.java
License:Apache License
private void put(JsonElement value) { if (pendingName != null) { if (!value.isJsonNull() || getSerializeNulls()) { JsonObject object = (JsonObject) peek(); object.add(pendingName, value); }/* ww w .j a v a 2 s . c om*/ pendingName = null; } else if (stack.isEmpty()) { product = value; } else { JsonElement element = peek(); if (element.isJsonArray()) { element.getAsJsonArray().add(value); } else { throw new IllegalStateException(); } } }