List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:io.thinger.thinger.views.BoolValue.java
License:Open Source License
@Override public void refreshContent(JsonElement element) { if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isBoolean()) { button.setChecked(element.getAsBoolean()); }//from w w w .ja v a 2 s . com }
From source file:io.thinger.thinger.views.DeviceResource.java
License:Open Source License
public DeviceResource(String resourceName, JsonElement resourceDescription) { this.resourceName = resourceName; resourceType = ResourceType.NONE;//from w w w. j a v a 2s . c o m if (resourceDescription.isJsonObject()) { JsonObject object = resourceDescription.getAsJsonObject(); if (object.has("fn")) { JsonElement function = object.get("fn"); if (function.isJsonPrimitive()) { JsonPrimitive value = function.getAsJsonPrimitive(); if (value.isNumber()) { resourceType = ResourceType.get(value.getAsInt()); } } } } }
From source file:io.thinger.thinger.views.Element.java
License:Open Source License
public static Element createElement(LinearLayout layout, JsonElement element, boolean output) { if (element.isJsonPrimitive()) { return createPrimitiveElement("", element.getAsJsonPrimitive(), layout, output); } else if (element.isJsonObject()) { return new ObjectValue(layout, element.getAsJsonObject(), output); }//from w ww. j ava 2s. c om return null; }
From source file:io.thinger.thinger.views.NumberValue.java
License:Open Source License
@Override public void refreshContent(JsonElement element) { if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) { editText.setText(element.getAsNumber().toString()); }/* w w w . jav a 2 s. c o m*/ }
From source file:io.thinger.thinger.views.StringValue.java
License:Open Source License
@Override public void refreshContent(JsonElement element) { if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) { editText.setText(element.getAsString()); } else {/* w ww. j a v a 2s. co m*/ editText.setText(""); } }
From source file:io.vertigo.ccc.console.JSonBeautifier.java
License:Apache License
private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) { int offset = inOffset; if (jsonElement.isJsonArray()) { offset++;/*from w ww . j ava 2s . c o m*/ for (Iterator<JsonElement> it = jsonElement.getAsJsonArray().iterator(); it.hasNext();) { //sb.append("- "); beautify(sb, it.next(), offset); } offset--; } else if (jsonElement.isJsonObject()) { for (Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { appendSpaces(sb, offset); sb.append(entry.getKey()); sb.append(" : "); if (entry.getValue().isJsonPrimitive()) { sb.append(entry.getValue()); appendCRLF(sb); } else { offset++; appendCRLF(sb); beautify(sb, entry.getValue(), offset); offset--; } } } else if (jsonElement.isJsonPrimitive()) { // appendSpaces(sb, offset); String s = jsonElement.toString(); if (s.startsWith("\"") && s.endsWith("\"")) { sb.append(s.substring(1, s.length() - 1)); } else { sb.append(s); } appendCRLF(sb); } else { sb.append("???"); //null } }
From source file:io.vertigo.shell.util.JSonBeautifier.java
License:Apache License
private static void beautify(final StringBuilder sb, final JsonElement jsonElement, final int inOffset) { int offset = inOffset; if (jsonElement.isJsonArray()) { offset++;/*from w w w . ja v a 2s. c om*/ for (final JsonElement jsonElement2 : jsonElement.getAsJsonArray()) { //sb.append("- "); beautify(sb, jsonElement2, offset); } offset--; } else if (jsonElement.isJsonObject()) { for (final Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { appendSpaces(sb, offset); sb.append(entry.getKey()); sb.append(" : "); if (entry.getValue().isJsonPrimitive()) { sb.append(entry.getValue()); appendCRLF(sb); } else { offset++; appendCRLF(sb); beautify(sb, entry.getValue(), offset); offset--; } } } else if (jsonElement.isJsonPrimitive()) { // appendSpaces(sb, offset); final String s = jsonElement.toString(); if (s.startsWith("\"") && s.endsWith("\"")) { sb.append(s.substring(1, s.length() - 1)); } else { sb.append(s); } appendCRLF(sb); } else { sb.append("???"); //null } }
From source file:io.webfolder.cdp.session.JavaScript.java
License:Open Source License
/** * Calls JavaScript function.//from w w w . java 2 s. co m * * <p> * Function must be declared at the global {@literal (window object)} scope. * You can use <strong>dot notation</strong> for function name. * </p> * * @param name function name * @param returnType return type of function * @param arguments function arguments * * @return function result */ @SuppressWarnings("unchecked") default <T> T callFunction(String name, Class<T> returnType, Object... arguments) { EvaluateResult windowResult = getThis().getCommand().getRuntime().evaluate("window"); if (windowResult == null) { return null; } if (windowResult.getExceptionDetails() != null && windowResult.getExceptionDetails().getException() != null) { getThis().releaseObject(windowResult.getExceptionDetails().getException().getObjectId()); throw new CdpException(windowResult.getExceptionDetails().getException().getDescription()); } CallArgument objArgument = new CallArgument(); objArgument.setValue(name); CallFunctionOnResult funcObj = getThis().getCommand().getRuntime().callFunctionOn( windowResult.getResult().getObjectId(), "function(functionName) { return functionName.split('.').reduce((o, i) => o[i], this); }", asList(objArgument), FALSE, FALSE, FALSE, FALSE, FALSE); getThis().releaseObject(windowResult.getResult().getObjectId()); if (funcObj.getExceptionDetails() != null && funcObj.getExceptionDetails().getException() != null) { getThis().releaseObject(funcObj.getExceptionDetails().getException().getObjectId()); throw new CdpException(funcObj.getExceptionDetails().getException().getDescription()); } if (ObjectType.Undefined.equals(funcObj.getResult().getType())) { getThis().releaseObject(funcObj.getResult().getObjectId()); throw new CdpException(format("Function [%s] is not defined", name)); } StringJoiner argNames = new StringJoiner(","); List<CallArgument> argsFunc = new ArrayList<>(); int i = 0; if (arguments != null && arguments.length > 0) { for (Object argument : arguments) { CallArgument ca = new CallArgument(); argsFunc.add(ca); if (argument != null) { if (getThis().isPrimitive(argument.getClass())) { ca.setValue(argument); } else { ca.setValue(getThis().getGson().toJson(argument)); } } argNames.add("arg" + (i + 1)); } } CallFunctionOnResult func = getThis().getCommand().getRuntime().callFunctionOn( funcObj.getResult().getObjectId(), format("function(%s) { const result = this.apply(this, Array.prototype.slice.call(arguments)); " + "return typeof result === 'undefined' ? undefined : JSON.stringify({ result : result }); }", argNames.toString()), argsFunc, FALSE, TRUE, FALSE, FALSE, FALSE); getThis().releaseObject(funcObj.getResult().getObjectId()); getThis().releaseObject(func.getResult().getObjectId()); if (func.getExceptionDetails() != null && func.getExceptionDetails().getException() != null) { getThis().releaseObject(func.getExceptionDetails().getException().getObjectId()); throw new CdpException(func.getExceptionDetails().getException().getDescription()); } Object value = null; if (ObjectType.String.equals(func.getResult().getType()) && !returnType.equals(void.class)) { String json = valueOf(func.getResult().getValue()); JsonObject object = getThis().getGson().fromJson(json, JsonObject.class); JsonElement result = object.get("result"); if (getThis().isPrimitive(returnType)) { value = getThis().getGson().fromJson(result, returnType); } else { if (result.isJsonPrimitive()) { value = getThis().getGson().fromJson(result.getAsString(), returnType); } } } else if (ObjectType.Undefined.equals(func.getResult().getType())) { value = void.class; } StringJoiner joiner = new StringJoiner("\", \""); for (Object o : arguments) { joiner.add(valueOf(o)); } getThis().logExit("callFunction", name + (arguments == null || arguments.length == 0 ? "" : "\", " + joiner.toString()), valueOf(value).replace("\n", "").replace("\r", "")); return !void.class.equals(value) ? (T) value : null; }
From source file:io.webfolder.cdp.session.JavaScript.java
License:Open Source License
/** * Gets JavaScript variable./* w ww . j a va 2s . c o m*/ * * <p> * Variable must be declared at the global {@literal (window object)} scope. * You can use <strong>dot notation</strong> for variable name. * </p> * * @param name variable name * @param returnType variable type * * @return variable value */ @SuppressWarnings("unchecked") public default <T> T getVariable(String name, Class<T> returnType) { EvaluateResult windowResult = getThis().getCommand().getRuntime().evaluate("window"); if (windowResult == null) { return null; } if (windowResult.getExceptionDetails() != null && windowResult.getExceptionDetails().getException() != null) { getThis().releaseObject(windowResult.getExceptionDetails().getException().getObjectId()); throw new CdpException(windowResult.getExceptionDetails().getException().getDescription()); } CallArgument objArgument = new CallArgument(); objArgument.setValue(name); CallFunctionOnResult obj = getThis().getCommand().getRuntime().callFunctionOn( windowResult.getResult().getObjectId(), "function(functionName) { const result = functionName.split('.').reduce((o, i) => o[i], this); " + "return typeof result === 'undefined' ? undefined : JSON.stringify({ result : result }); }", asList(objArgument), FALSE, FALSE, FALSE, FALSE, FALSE); getThis().releaseObject(windowResult.getResult().getObjectId()); if (obj.getExceptionDetails() != null && obj.getExceptionDetails().getException() != null) { getThis().releaseObject(obj.getExceptionDetails().getException().getObjectId()); throw new CdpException(obj.getExceptionDetails().getException().getDescription()); } if (ObjectType.Undefined.equals(obj.getResult().getType())) { getThis().releaseObject(obj.getResult().getObjectId()); throw new CdpException(format("Variable [%s] is not defined", name)); } Object value = null; if (ObjectType.String.equals(obj.getResult().getType()) && !returnType.equals(void.class)) { String json = valueOf(obj.getResult().getValue()); JsonObject object = getThis().getGson().fromJson(json, JsonObject.class); JsonElement result = object.get("result"); if (getThis().isPrimitive(returnType)) { value = getThis().getGson().fromJson(result, returnType); } else { if (result.isJsonPrimitive()) { value = getThis().getGson().fromJson(result.getAsString(), returnType); } } } else if (ObjectType.Undefined.equals(obj.getResult().getType())) { value = void.class; } getThis().releaseObject(obj.getResult().getObjectId()); return (T) value; }
From source file:io.webfolder.cdp.session.SessionInvocationHandler.java
License:Open Source License
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if (!session.isConnected()) { log.error("Unable to invoke {}.{}()", new Object[] { method.getDeclaringClass().getName(), method.getName() }); throw new CdpException("WebSocket connection is not alive"); }/*from www .jav a 2s.c o m*/ final Class<?> klass = method.getDeclaringClass(); final String domain = klass.getAnnotation(Domain.class).value(); final String command = method.getName(); boolean hasArgs = args != null && args.length > 0; Map<String, Object> params = hasArgs ? new HashMap<>(args.length) : emptyMap(); if (hasArgs) { int argIndex = 0; Parameter[] parameters = method.getParameters(); for (Object argValue : args) { String argName = parameters[argIndex++].getName(); params.put(argName, argValue); } } int id = counter.incrementAndGet(); Map<String, Object> map = new HashMap<>(3); map.put("id", id); map.put("method", format("%s.%s", domain, command)); map.put("params", params); String json = gson.toJson(map); log.debug(json); WSContext context = new WSContext(); contextList.put(id, context); webSocket.sendText(json); context.await(); if (context.getError() != null) { throw context.getError(); } Class<?> retType = method.getReturnType(); if (retType.equals(void.class) || retType.equals(Void.class)) { return null; } JsonElement data = context.getData(); String returns = method.isAnnotationPresent(Returns.class) ? method.getAnnotation(Returns.class).value() : null; if (data == null) { return null; } if (!data.isJsonObject()) { throw new CdpException("invalid response"); } JsonObject object = data.getAsJsonObject(); JsonElement result = object.get("result"); if (result == null || !result.isJsonObject()) { throw new CdpException("invalid result"); } JsonObject resultObject = result.getAsJsonObject(); Object ret = null; Type genericReturnType = method.getGenericReturnType(); if (returns != null) { JsonElement jsonElement = resultObject.get(returns); if (jsonElement != null && jsonElement.isJsonPrimitive()) { if (String.class.equals(retType)) { return resultObject.get(returns).getAsString(); } else if (Boolean.class.equals(retType)) { return resultObject.get(returns).getAsBoolean() ? Boolean.TRUE : Boolean.FALSE; } else if (Integer.class.equals(retType)) { return new Integer(resultObject.get(returns).getAsInt()); } else if (Double.class.equals(retType)) { return new Double(resultObject.get(returns).getAsDouble()); } } if (jsonElement != null && byte[].class.equals(genericReturnType)) { String encoded = gson.fromJson(jsonElement, String.class); if (encoded == null || encoded.trim().isEmpty()) { return null; } else { return getDecoder().decode(encoded); } } if (List.class.equals(retType)) { JsonArray jsonArray = jsonElement.getAsJsonArray(); ret = gson.fromJson(jsonArray, genericReturnType); } else { ret = gson.fromJson(jsonElement, genericReturnType); } } else { ret = gson.fromJson(resultObject, genericReturnType); } return ret; }