List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:org.structr.core.rest.JsonInputGSONAdapter.java
License:Open Source License
public static JsonInput deserialize(final JsonElement json, final JsonDeserializationContext context) throws JsonParseException { final JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { final JsonObject obj = json.getAsJsonObject(); for (final Entry<String, JsonElement> entry : obj.entrySet()) { final String key = entry.getKey(); final JsonElement elem = entry.getValue(); if (elem.isJsonNull()) { wrapper.add(key, null);// ww w .j a v a 2 s.com } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, context)); } else if (elem.isJsonArray()) { final JsonArray array = elem.getAsJsonArray(); final List list = new LinkedList(); for (final JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive())); } } } else if (json.isJsonArray()) { final JsonArray array = json.getAsJsonArray(); for (final JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive())); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, context)); } } } else { // when we arrive here, the input element was // not one of the expected types => error throw new JsonSyntaxException("Invalid JSON, expecting object or array"); } return wrapper; }
From source file:org.structr.rest.JsonInputGSONAdapter.java
License:Open Source License
private JsonInput deserialize(JsonElement json, JsonDeserializationContext context) throws JsonParseException { JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { JsonObject obj = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement elem = entry.getValue(); if (elem.isJsonNull()) { wrapper.add(key, null);//w w w . j a v a 2 s . co m } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, context)); } else if (elem.isJsonArray()) { final JsonArray array = elem.getAsJsonArray(); final List list = new LinkedList(); for (JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive())); } } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); for (JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive())); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, context)); } } } return wrapper; }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ActionTypeAdapterFactory.java
License:Apache License
@Override protected void beforeWrite(Action source, JsonElement toSerialize) { if (toSerialize == null || toSerialize.isJsonNull()) { return;/*from w w w .j av a 2 s . c o m*/ } JsonObject actionObj = toSerialize.getAsJsonObject(); if (actionObj == null || actionObj.isJsonNull()) { return; } // rewrite parameters from a nested object (map) to properties on the action JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS); if (paramsElem != null && !paramsElem.isJsonNull()) { JsonObject paramsObj = paramsElem.getAsJsonObject(); actionObj.remove(PROPERTY_PARAMETERS); for (Entry<String, JsonElement> entry : paramsObj.entrySet()) { actionObj.add(entry.getKey(), entry.getValue()); } } }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ActionTypeAdapterFactory.java
License:Apache License
@Override protected void afterRead(JsonElement deserialized) { if (deserialized == null || deserialized.isJsonNull()) { return;// w w w. j a va 2 s . c o m } JsonObject actionObj = deserialized.getAsJsonObject(); if (actionObj == null || actionObj.isJsonNull()) { return; } if (actionObj.has(PROPERTY_PARAMETERS)) { throw new IllegalArgumentException("Attempting to deserialize Action that contains a colliding " + PROPERTY_PARAMETERS + " property"); } // temporary list of field names List<String> fields = new ArrayList<>(); // rewrite parameters to a nested object (map) JsonObject paramsObj = new JsonObject(); for (Entry<String, JsonElement> entry : actionObj.entrySet()) { // skip ID field if (PROPERTY_ID.equals(entry.getKey())) { continue; } String paramId = entry.getKey(); fields.add(paramId); // to be removed later paramsObj.add(paramId, entry.getValue()); } actionObj.add(PROPERTY_PARAMETERS, paramsObj); // remove all fields other than the list fields.forEach(field -> actionObj.remove(field)); }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ApplicationTypeAdapterFactory.java
License:Apache License
@Override protected void beforeWrite(Application source, JsonElement toSerialize) { if (toSerialize == null || toSerialize.isJsonNull()) { return;/*from w w w . j a v a 2 s. co m*/ } JsonObject appObj = toSerialize.getAsJsonObject(); if (appObj == null || appObj.isJsonNull()) { return; } // remove widget IDs JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS); if (widgetsElem != null && !widgetsElem.isJsonNull()) { for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) { JsonElement widgetElem = entry.getValue(); if (widgetElem == null || widgetElem.isJsonNull()) { continue; } widgetElem.getAsJsonObject().remove(PROPERTY_ID); } } // remove action IDs JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS); if (actionsElem != null && !actionsElem.isJsonNull()) { for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) { JsonElement actionElem = entry.getValue(); if (actionElem == null || actionElem.isJsonNull()) { continue; } actionElem.getAsJsonObject().remove(PROPERTY_ID); } } }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ApplicationTypeAdapterFactory.java
License:Apache License
@Override protected void afterRead(JsonElement deserialized) { if (deserialized == null || deserialized.isJsonNull()) { return;//from www . j ava2 s . c o m } JsonObject appObj = deserialized.getAsJsonObject(); if (appObj == null || appObj.isJsonNull()) { return; } // inject widget IDs JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS); if (widgetsElem != null && !widgetsElem.isJsonNull()) { for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) { JsonElement widgetElem = entry.getValue(); if (widgetElem == null || widgetElem.isJsonNull()) { continue; } widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey()); } } // inject action IDs JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS); if (actionsElem != null && !actionsElem.isJsonNull()) { for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) { JsonElement actionElem = entry.getValue(); if (actionElem == null || actionElem.isJsonNull()) { continue; } actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey()); } } }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.UpdateActionTypeAdapterFactory.java
License:Apache License
@Override protected void beforeWrite(UpdateAction source, JsonElement toSerialize) { if (toSerialize == null || toSerialize.isJsonNull()) { return;/*from w ww . jav a 2 s. co m*/ } JsonObject actionObj = toSerialize.getAsJsonObject(); if (actionObj == null || actionObj.isJsonNull()) { return; } // rewrite parameters map from {name => Parameter} to {name => value} JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS); if (paramsElem != null && !paramsElem.isJsonNull()) { JsonObject paramsObj = paramsElem.getAsJsonObject(); actionObj.remove(PROPERTY_PARAMETERS); JsonObject newParamsObj = new JsonObject(); for (Entry<String, JsonElement> entry : paramsObj.entrySet()) { newParamsObj.add(entry.getKey(), entry.getValue().getAsJsonObject().getAsJsonPrimitive(PROPERTY_VALUE)); } actionObj.add(PROPERTY_PARAMETERS, newParamsObj); } }
From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java
License:Apache License
private static CharFilterFactory[] buildCharFilters(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }// w w w. ja va 2 s. com if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for charfilters, but got:" + el.toString() + " for " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in char filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in char filter factory builder in:" + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis."); JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); for (Map.Entry<String, String> e : params.entrySet()) { System.out.println("PARAM: " + e.getKey() + " : " + e.getValue()); } String spiName = ""; for (String s : CharFilterFactory.availableCharFilters()) { Class clazz = CharFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } try { CharFilterFactory charFilterFactory = CharFilterFactory.forName(spiName, params); if (charFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) charFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(charFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While trying to load " + analyzerName + ": " + e.getMessage(), e); } } if (ret.size() == 0) { return new CharFilterFactory[0]; } return ret.toArray(new CharFilterFactory[ret.size()]); }
From source file:org.tallison.gramreaper.ingest.schema.AnalyzerDeserializer.java
License:Apache License
private static TokenFilterFactory[] buildTokenFilterFactories(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }/* w ww . j a v a 2 s. c o m*/ if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for tokenfilters, but got:" + el.toString() + " in " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<TokenFilterFactory> ret = new LinkedList<>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in token filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in token filter factory builder in " + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : TokenFilterFactory.availableTokenFilters()) { Class clazz = TokenFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } try { TokenFilterFactory tokenFilterFactory = TokenFilterFactory.forName(spiName, params); if (tokenFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokenFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(tokenFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While loading " + analyzerName, e); } } if (ret.size() == 0) { return new TokenFilterFactory[0]; } return ret.toArray(new TokenFilterFactory[ret.size()]); }
From source file:org.terasology.persistence.typeHandling.gson.GsonTypeHandlerAdapter.java
License:Apache License
@Override public T read(JsonReader in) throws IOException { JsonElement value = Streams.parse(in); if (value.isJsonNull()) { return null; }/* w w w.j a v a 2 s . c o m*/ return deserializer.deserialize(value, typeToken.getType(), (JsonDeserializationContext) ReflectionUtil.readField(gson, "deserializationContext")); }