List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:ai.api.model.Result.java
License:Apache License
void trimParameters() { if (parameters != null) { final List<String> parametersToTrim = new LinkedList<String>(); for (final String key : parameters.keySet()) { final JsonElement jsonElement = parameters.get(key); if (jsonElement != null && jsonElement.isJsonPrimitive()) { if (((JsonPrimitive) jsonElement).isString() && TextUtils.isEmpty(jsonElement.getAsString())) { parametersToTrim.add(key); }// w ww . j a v a2 s .c o m } } for (final String key : parametersToTrim) { parameters.remove(key); } } }
From source file:angularBeans.remote.InvocationHandler.java
License:LGPL
private void genericInvoke(Object service, String methodName, JsonObject params, Map<String, Object> returns, long reqID, String UID, HttpServletRequest request) throws SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { Object mainReturn = null;/* w ww. j av a 2s.c om*/ Method m = null; JsonElement argsElem = params.get("args"); if (reqID > 0) { returns.put("reqId", reqID); } if (argsElem != null) { JsonArray args = params.get("args").getAsJsonArray(); for (Method mt : service.getClass().getMethods()) { if (mt.getName().equals(methodName)) { m = mt; Type[] parameters = mt.getParameterTypes(); if (parameters.length == args.size()) { List<Object> argsValues = new ArrayList<>(); for (int i = 0; i < parameters.length; i++) { Class typeClass; String typeString = ((parameters[i]).toString()); if (typeString.startsWith("interface")) { typeString = typeString.substring(10); typeClass = Class.forName(typeString); } else { if (typeString.startsWith("class")) { typeString = typeString.substring(6); typeClass = Class.forName(typeString); } else { typeClass = builtInMap.get(typeString); } } JsonElement element = args.get(i); if (element.isJsonPrimitive()) { String val = element.getAsString(); argsValues.add(CommonUtils.convertFromString(val, typeClass)); } else if (element.isJsonArray()) { JsonArray arr = element.getAsJsonArray(); argsValues.add(util.deserialise(arrayTypesMap.get(typeString), arr)); } else { argsValues.add(util.deserialise(typeClass, element)); } } if (!CommonUtils.isGetter(mt)) { update(service, params); } try { mainReturn = mt.invoke(service, argsValues.toArray()); } catch (Exception e) { handleException(mt, e); e.printStackTrace(); } } } } } else { for (Method mt : service.getClass().getMethods()) { if (mt.getName().equals(methodName)) { Type[] parameters = mt.getParameterTypes(); // handling methods that took HttpServletRequest as parameter if (parameters.length == 1) { // if(mt.getParameters()[0].getType()==HttpServletRequest.class) // { // System.out.println("hehe..."); // mt.invoke(service, request); // // } } else { if (!CommonUtils.isGetter(m)) { update(service, params); } mainReturn = mt.invoke(service); } } } } ModelQueryImpl qImpl = (ModelQueryImpl) modelQueryFactory.get(service.getClass()); Map<String, Object> scMap = new HashMap<>(qImpl.getData()); returns.putAll(scMap); qImpl.getData().clear(); if (!modelQueryFactory.getRootScope().getRootScopeMap().isEmpty()) { returns.put("rootScope", new HashMap<>(modelQueryFactory.getRootScope().getRootScopeMap())); modelQueryFactory.getRootScope().getRootScopeMap().clear(); } String[] updates = null; if (m.isAnnotationPresent(NGReturn.class)) { if (mainReturn == null) mainReturn = ""; NGReturn ngReturn = m.getAnnotation(NGReturn.class); updates = ngReturn.updates(); if (ngReturn.model().length() > 0) { returns.put(ngReturn.model(), mainReturn); Map<String, String> binding = new HashMap<>(); binding.put("boundTo", ngReturn.model()); mainReturn = binding; } } if (m.isAnnotationPresent(NGPostConstruct.class)) { NGPostConstruct ngPostConstruct = m.getAnnotation(NGPostConstruct.class); updates = ngPostConstruct.updates(); } if (updates != null) { if ((updates.length == 1) && (updates[0].equals("*"))) { List<String> upd = new ArrayList<>(); for (Method met : service.getClass().getDeclaredMethods()) { if (CommonUtils.isGetter(met)) { String fieldName = (met.getName()).substring(3); String firstCar = fieldName.substring(0, 1); upd.add((firstCar.toLowerCase() + fieldName.substring(1))); } } updates = new String[upd.size()]; for (int i = 0; i < upd.size(); i++) { updates[i] = upd.get(i); } } } if (updates != null) { for (String up : updates) { String getterName = GETTER_PREFIX + up.substring(0, 1).toUpperCase() + up.substring(1); Method getter; try { getter = service.getClass().getMethod(getterName); } catch (NoSuchMethodException e) { getter = service.getClass() .getMethod((getterName.replace(GETTER_PREFIX, BOOLEAN_GETTER_PREFIX))); } Object result = getter.invoke(service); returns.put(up, result); } } returns.put("mainReturn", mainReturn); if (!logger.getLogPool().isEmpty()) { returns.put("log", logger.getLogPool().toArray()); logger.getLogPool().clear(); } }
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; }/*www .ja v a 2 s . c om*/ 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:ar.com.ws.djnextension.config.GsonBuilderConfiguratorForTesting.java
License:Open Source License
/** * @param parent// w w w . j a va 2s .c om * @param elementName * @return */ private static int getIntValue(JsonObject parent, String elementName) { assert parent != null; assert !StringUtils.isEmpty(elementName); JsonElement element = parent.get(elementName); if (!element.isJsonPrimitive()) { throw new JsonParseException("Element + '" + elementName + "' must be a valid integer"); } JsonPrimitive primitiveElement = (JsonPrimitive) element; if (!primitiveElement.isNumber()) { throw new JsonParseException("Element + '" + elementName + "' must be a valid integer"); } return primitiveElement.getAsInt(); }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }/*from ww w . j a va 2 s .co m*/ 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:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
/** * Appends key and/or value to json//from w ww. j a v a 2s .c om * * @param json string json which will be modified * @param key key that will be appended * @param value value that will be appended * @return the specified JSON string with appended key and/or value */ public static String appendValueToJson(String json, String key, Object value) { JsonElement jsonElement = parseObject(json); if (jsonElement != null) { if (jsonElement.isJsonPrimitive()) { JsonArray jsonArray = new JsonArray(); jsonArray.add(jsonElement); jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } else if (jsonElement.isJsonObject()) { if (key == null) { throw new IllegalArgumentException( "to append some value into a JsonObject the 'key' parameter must not be null"); } JsonObject jsonObject = (JsonObject) jsonElement; jsonObject.add(key, objectToJsonElementTree(value)); return jsonObject.toString(); } else if (jsonElement.isJsonArray()) { JsonArray jsonArray = (JsonArray) jsonElement; jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } } return getJsonElementAsString(jsonElement); }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static String getJsonElementAsString(JsonElement jsonElement) { if (jsonElement == null) { return JsonNull.INSTANCE.toString(); }//from ww w. j a v a2 s . c o m if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsString(); } return jsonElement.toString(); }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation// www. j av a 2 s. c o m * * @param dataToFormat data to be formatted * @param donation the donation data * * @return the fully var-replaced JsonElement */ public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) { if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) { return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward)); } if (dataToFormat.isJsonArray()) { JsonArray out = new JsonArray(); for (JsonElement element : dataToFormat.getAsJsonArray()) { out.add(formatText(element, donation, reward)); } return out; } if (dataToFormat.isJsonObject()) { JsonObject out = new JsonObject(); for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) { out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward)); } return out; } return dataToFormat; }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
public static NBTBase parseJSON(JsonElement element) { if (element.isJsonObject()) return parseJSON(element.getAsJsonObject()); else if (element.isJsonArray()) return parseJSON(element.getAsJsonArray()); else if (element.isJsonPrimitive()) return parseJSON(element.getAsJsonPrimitive()); return null;/*from ww w . j a va2 s. c om*/ }
From source file:ccm.pay2spawn.util.JsonNBTHelper.java
License:Open Source License
public static JsonElement fixNulls(JsonElement element) { if (element.isJsonNull()) return new JsonPrimitive(""); if (element.isJsonObject()) return fixNulls(element.getAsJsonObject()); if (element.isJsonArray()) return fixNulls(element.getAsJsonArray()); if (element.isJsonPrimitive()) return fixNulls(element.getAsJsonPrimitive()); return null;/*from w ww . j a v a2s.co m*/ }