List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:org.jenkinsci.plugins.relution_publisher.util.Json.java
License:Apache License
public static String getString(final JsonObject object, final String memberName) { final JsonElement element = object.get(memberName); if (element == null || !element.isJsonPrimitive()) { return null; }/*from w w w .jav a 2s .c o m*/ return element.getAsString(); }
From source file:org.jenkinsci.plugins.relution_publisher.util.Json.java
License:Apache License
public static int getInt(final JsonObject object, final String memberName) { final JsonElement element = object.get(memberName); if (element == null || !element.isJsonPrimitive()) { return 0; }//from w w w. j a v a 2s .com return element.getAsInt(); }
From source file:org.jenkinsci.plugins.relution_publisher.util.Json.java
License:Apache License
public static Integer getInteger(final JsonObject object, final String memberName) { final JsonElement element = object.get(memberName); if (element == null || !element.isJsonPrimitive()) { return null; }//from w w w . j ava 2 s .c om return element.getAsInt(); }
From source file:org.jsonddl.generator.InferSchema.java
License:Apache License
private Type inferType(String propertyName, JsonElement node) { if (node.isJsonPrimitive()) { JsonPrimitive p = node.getAsJsonPrimitive(); if (p.isBoolean()) { return new Type.Builder().withKind(Kind.BOOLEAN).build(); }/* w w w. j a va 2 s .c om*/ if (p.isNumber()) { return new Type.Builder().withKind(Kind.DOUBLE).build(); } if (p.isString()) { return new Type.Builder().withKind(Kind.STRING).build(); } throw new RuntimeException("Unhandled primitive type " + p.toString()); } if (node.isJsonObject()) { JsonObject object = node.getAsJsonObject(); return inferFromObject(propertyName, object); } if (node.isJsonArray()) { return inferFromArray(propertyName, node.getAsJsonArray()); } throw new RuntimeException("Could not infer type from node " + node.toString()); }
From source file:org.kairosdb.core.http.rest.json.DataPointsParser.java
License:Apache License
private String findType(JsonElement value) { checkState(value.isJsonPrimitive()); JsonPrimitive primitiveValue = (JsonPrimitive) value; if (primitiveValue.isNumber() || (primitiveValue.isString() && Util.isNumber(value.getAsString()))) { String v = value.getAsString(); if (!v.contains(".")) { return "long"; } else {/* www. ja v a2s. c o m*/ return "double"; } } else return "string"; }
From source file:org.kurento.commons.BasicJsonUtils.java
License:Apache License
private static Object convertValue(JsonElement value) { if (value.isJsonNull()) { return null; } else if (value.isJsonPrimitive()) { JsonPrimitive prim = value.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isNumber()) { Number n = prim.getAsNumber(); if (n.doubleValue() == n.intValue()) { return n.intValue(); } else { return n.doubleValue(); }// w ww . j a v a 2 s. co m } else if (prim.isString()) { return prim.getAsString(); } else { throw new RuntimeException("Unrecognized value: " + value); } } else { return value.toString(); } }
From source file:org.kurento.room.client.internal.JsonRoomUtils.java
License:Apache License
@SuppressWarnings("unchecked") private static <T> T getConverted(JsonElement paramValue, String property, Class<T> type, boolean allowNull) { if (paramValue == null) { if (allowNull) { return null; } else {/*w ww . j ava2 s .com*/ throw new RoomException(Code.TRANSPORT_ERROR_CODE, "Invalid method lacking parameter '" + property + "'"); } } if (type == String.class) { if (paramValue.isJsonPrimitive()) { return (T) paramValue.getAsString(); } } if (type == Integer.class) { if (paramValue.isJsonPrimitive()) { return (T) Integer.valueOf(paramValue.getAsInt()); } } if (type == JsonArray.class) { if (paramValue.isJsonArray()) { return (T) paramValue.getAsJsonArray(); } } throw new RoomException(Code.TRANSPORT_ERROR_CODE, "Param '" + property + "' with value '" + paramValue + "' is not a " + type.getName()); }
From source file:org.kurento.tree.client.internal.JsonTreeUtils.java
License:Apache License
@SuppressWarnings("unchecked") private static <T> T getConverted(JsonElement paramValue, String property, Class<T> type, boolean allowNull) { if (paramValue == null || paramValue instanceof JsonNull) { if (allowNull) { return null; } else {/*from w w w . j a va2 s .co m*/ throw new JsonRpcErrorException(1, "Invalid method lacking parameter '" + property + "'"); } } if (type == String.class) { if (paramValue.isJsonPrimitive()) { return (T) paramValue.getAsString(); } } if (type == Integer.class) { if (paramValue.isJsonPrimitive()) { return (T) Integer.valueOf(paramValue.getAsInt()); } } throw new JsonRpcErrorException(2, "Param '" + property + "' with value '" + paramValue + "' is not a " + type.getName()); }
From source file:org.kurento.tree.server.app.RegistrarJsonRpcHandler.java
License:Apache License
@SuppressWarnings("unchecked") public <T> T getStringParam(Request<JsonObject> request, String paramName) { JsonElement paramValue = request.getParams().get(paramName); if (paramValue == null) { throw new JsonRpcErrorException(1, "Invalid request lacking parameter '" + paramName + "'"); }//www .j a va 2 s.co m if (paramValue.isJsonPrimitive()) { return (T) paramValue.getAsString(); } throw new JsonRpcErrorException(2, "Param '" + paramName + " with value '" + paramValue + "' is not a String"); }
From source file:org.lanternpowered.server.data.translator.JsonTranslator.java
License:MIT License
private Object fromJson(@Nullable DataView container, JsonElement json) { if (json.isJsonObject()) { if (container == null) { container = new MemoryDataContainer(DataView.SafetyMode.NO_DATA_CLONED); }/*w w w . ja v a2s . co m*/ for (Entry<String, JsonElement> en : json.getAsJsonObject().entrySet()) { String key = en.getKey(); JsonElement element = en.getValue(); if (element.isJsonObject()) { this.fromJson(container.createView(DataQuery.of(key)), element); } else { container.set(DataQuery.of(key), this.fromJson(null, json)); } } return container; } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); List<Object> objects = Lists.newArrayListWithCapacity(array.size()); int ints = 0; int bytes = 0; for (int i = 0; i < array.size(); i++) { Object object = this.fromJson(null, array.get(i)); objects.add(object); if (object instanceof Integer) { ints++; } if (object instanceof Byte) { bytes++; } } if (bytes == objects.size()) { Byte[] array0 = new Byte[bytes]; for (int i = 0; i < bytes; i++) { array0[i] = (Byte) objects.get(i); } return array0; } else if (ints == objects.size()) { Integer[] array0 = new Integer[ints]; for (int i = 0; i < bytes; i++) { array0[i] = (Integer) objects.get(i); } return array0; } else { return objects; } } else if (json.isJsonPrimitive()) { String value = json.getAsString(); if (DOUBLE.matcher(value).matches()) { return Double.parseDouble(value.substring(0, value.length() - 1)); } else if (FLOAT.matcher(value).matches()) { return Float.parseFloat(value.substring(0, value.length() - 1)); } else if (LONG.matcher(value).matches()) { return Long.parseLong(value.substring(0, value.length() - 1)); } else if (SHORT.matcher(value).matches()) { return Short.parseShort(value.substring(0, value.length() - 1)); } else if (BYTE.matcher(value).matches()) { return Byte.parseByte(value.substring(0, value.length() - 1)); } else if (INTEGER.matcher(value).matches()) { return Integer.parseInt(value); } else if (DOUBLE_UNTYPED.matcher(value).matches()) { return Double.parseDouble(value); } else { if ("true".equalsIgnoreCase(value)) { return true; } else if ("false".equalsIgnoreCase(value)) { return false; } return value; } } return json; }