List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcUnmarshaller.java
License:Open Source License
private Object getInnerItem(JsonElement jsonElement) { if (jsonElement.isJsonNull()) { return null; }//from w w w .j a v a 2 s . co m if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject(); } if (jsonElement.isJsonPrimitive()) { JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); if (jsonPrimitive.isNumber()) { return jsonPrimitive.getAsDouble(); } else if (jsonPrimitive.isString()) { return jsonPrimitive.getAsString(); } else { return jsonPrimitive.getAsBoolean(); } } throw new IllegalStateException("Unexpected json element type"); }
From source file:org.eclipse.che.api.languageserver.util.EitherUtil.java
License:Open Source License
private static boolean matches(JsonElement element, JsonDecision decision) { if (decision == JsonDecision.LIST) { return element.isJsonArray(); }// w ww . jav a 2 s . c o m if (decision == JsonDecision.BOOLEAN) { return element.isJsonPrimitive() && ((JsonPrimitive) element).isBoolean(); } if (decision == JsonDecision.NUMBER) { return element.isJsonPrimitive() && ((JsonPrimitive) element).isNumber(); } if (decision == JsonDecision.STRING) { return element.isJsonPrimitive() && ((JsonPrimitive) element).isString(); } return element.isJsonObject(); }
From source file:org.eclipse.eavp.viz.service.paraview.widgets.ParaViewCanvas.java
License:Open Source License
/** * Sends an update request to the specified client. This operation waits for * the response, after which it will construct an Image from the encoded * image string. If the returned image is stale, then {@link #stale} is set * to true.//from w w w. j a v a 2 s . c o m * <p> * <b>Note:</b> This operation is intended to be called from the refresh * thread in {@link #refreshRunnable}. * </p> * * @param client * The client from which to request a new image. * @param viewId * The ID of the view to render on the client. * @param width * The width of the Canvas when making the request. * @param height * The height of the Canvas when making the request. * @return An Image from the client, or {@code null} if the render request * could not be completed. */ private Image refreshClient(IParaViewWebClient client, int viewId, int width, int height) { // Set the default return value. Image image = null; if (client != null && width > 0 && height > 0) { // The request to draw will return an object containing an encoded // image string and a flag stating whether the image is stale. // Send a render request to the client and wait for the reply. JsonObject response = null; try { response = client.render(viewId, IMAGE_QUALITY, width, height).get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } // If the response was received, try to read in the encoded image // and the stale flag. if (response != null) { // Read the base 64 image string from the response, then // construct a new Image from the encoded string. JsonElement element = response.get("image"); if (element != null && element.isJsonPrimitive()) { try { String base64Image = element.getAsString(); // TODO When we start using Java 8, replace the // DatatypeConverter with the java.util.Base64 // class. byte[] decode = DatatypeConverter.parseBase64Binary(base64Image); // byte[] decode = Base64.getDecoder().decode( // base64Image.getBytes()); ByteArrayInputStream inputStream = new ByteArrayInputStream(decode); // Load the input stream into a new Image. ImageData[] data = new ImageLoader().load(inputStream); if (data.length > 0) { image = new Image(getDisplay(), data[0]); } } catch (ClassCastException e) { // Could not read the image. } } // If the image is stale, trigger another refresh operation. element = response.get("stale"); if (element != null && element.isJsonPrimitive()) { try { if (element.getAsBoolean()) { refresh(); } } catch (ClassCastException e) { // Could not read the stale variable. } } } } return image; }
From source file:org.eclipse.jgit.diff.EditDeserializer.java
License:Apache License
private static int get(final JsonArray a, final int idx) throws JsonParseException { final JsonElement v = a.get(idx); if (!v.isJsonPrimitive()) { throw new JsonParseException("Expected array of 4 for Edit type"); }//from w ww . ja v a 2 s .com final JsonPrimitive p = (JsonPrimitive) v; if (!p.isNumber()) { throw new JsonParseException("Expected array of 4 for Edit type"); } return p.getAsInt(); }
From source file:org.eclipse.milo.opcua.binaryschema.gson.JsonStructureCodec.java
License:Open Source License
@Override protected Object memberTypeToOpcUaScalar(JsonElement member, String typeName) { if (member == null || member.isJsonNull()) { return null; } else if (member.isJsonArray()) { JsonArray array = member.getAsJsonArray(); switch (typeName) { case "ByteString": { byte[] bs = new byte[array.size()]; for (int i = 0; i < array.size(); i++) { bs[i] = array.get(i).getAsByte(); }/*from www.j ava 2s . c o m*/ return ByteString.of(bs); } default: return array; } } else if (member.isJsonObject()) { JsonObject jsonObject = member.getAsJsonObject(); switch (typeName) { case "QualifiedName": { return new QualifiedName(jsonObject.get("namespaceIndex").getAsInt(), jsonObject.get("name").getAsString()); } case "LocalizedText": { return new LocalizedText(jsonObject.get("locale").getAsString(), jsonObject.get("text").getAsString()); } default: return jsonObject; } } else if (member.isJsonPrimitive()) { JsonPrimitive primitive = member.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { switch (typeName) { case "Guid": return UUID.fromString(primitive.getAsString()); case "NodeId": return NodeId.parseSafe(primitive.getAsString()).orElse(NodeId.NULL_VALUE); case "ExpandedNodeId": return ExpandedNodeId.parse(primitive.getAsString()); case "XmlElement": return new XmlElement(primitive.getAsString()); default: return primitive.getAsString(); } } else if (primitive.isNumber()) { switch (typeName) { case "SByte": return primitive.getAsByte(); case "Int16": return primitive.getAsShort(); case "Int32": return primitive.getAsInt(); case "Int64": return primitive.getAsLong(); case "Byte": return ubyte(primitive.getAsShort()); case "UInt16": return ushort(primitive.getAsInt()); case "UInt32": return uint(primitive.getAsLong()); case "UInt64": return ulong(primitive.getAsBigInteger()); case "Float": return primitive.getAsFloat(); case "Double": return primitive.getAsDouble(); case "DateTime": return new DateTime(primitive.getAsLong()); case "StatusCode": return new StatusCode(primitive.getAsLong()); default: return primitive.getAsNumber(); } } } return null; }
From source file:org.eclipse.php.composer.api.json.JsonParser.java
License:Open Source License
private Object buildTree(JsonElement entity) { if (entity.isJsonPrimitive()) { JsonPrimitive p = entity.getAsJsonPrimitive(); if (p.isBoolean()) { return p.getAsBoolean(); }/* w w w .jav a 2 s .co m*/ if (p.isNumber()) { return p.getAsLong(); } return p.getAsString(); } else if (entity.isJsonNull()) { return null; } else if (entity.isJsonArray()) { LinkedList<Object> arr = new LinkedList<Object>(); for (JsonElement el : entity.getAsJsonArray()) { arr.add(buildTree(el)); } return arr; } else if (entity.isJsonObject()) { LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); for (Entry<String, JsonElement> el : entity.getAsJsonObject().entrySet()) { map.put(el.getKey(), buildTree(el.getValue())); } return map; } return null; }
From source file:org.eclipse.scada.base.json.VariantJsonDeserializer.java
License:Open Source License
@Override public Variant deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }/*from w w w . j a v a 2 s.co m*/ if (json instanceof JsonPrimitive) { return decodeFromPrimitive(json); } if (json instanceof JsonObject) { final JsonObject jsonObj = (JsonObject) json; final JsonElement type = jsonObj.get(VariantJson.FIELD_TYPE); final JsonElement value = jsonObj.get(VariantJson.FIELD_VALUE); if (type == null || type.isJsonNull()) { if (value == null) { throw new JsonParseException(String.format("Variant encoded as object must have a field '%s'", VariantJson.FIELD_VALUE)); } return Variant.valueOf(value.getAsString()); } if (!type.isJsonPrimitive()) { throw new JsonParseException( String.format("Variant field '%s' must be a string containing the variant type (%s)", VariantJson.FIELD_TYPE, StringHelper.join(VariantType.values(), ", "))); } final String typeStr = type.getAsString(); if (typeStr.equals("NULL")) { return Variant.NULL; } if (value == null || value.isJsonNull()) { throw new JsonParseException(String.format( "The type '%s' does not support a null value. Use variant type NULL instead.", typeStr)); } if (value.isJsonObject() || value.isJsonArray()) { throw new JsonParseException( "The variant value must be a JSON primitive matching the type. Arrays and objects are not supported"); } switch (type.getAsString()) { case "BOOLEAN": return Variant.valueOf(value.getAsBoolean()); case "STRING": return Variant.valueOf(value.getAsString()); case "DOUBLE": return Variant.valueOf(value.getAsDouble()); case "INT32": return Variant.valueOf(value.getAsInt()); case "INT64": return Variant.valueOf(value.getAsLong()); default: throw new JsonParseException(String.format("Type '%s' is unknown (known types: %s)", StringHelper.join(VariantType.values(), ", "))); } } throw new JsonParseException("Unknown serialization of Variant type"); }
From source file:org.eclipse.smarthome.config.core.ConfigurationDeserializer.java
License:Open Source License
private Configuration deserialize(JsonObject propertiesObject) { Configuration configuration = new Configuration(); for (Entry<String, JsonElement> entry : propertiesObject.entrySet()) { JsonElement value = entry.getValue(); String key = entry.getKey(); if (value.isJsonPrimitive()) { JsonPrimitive primitive = value.getAsJsonPrimitive(); configuration.put(key, deserialize(primitive)); } else if (value.isJsonArray()) { JsonArray array = value.getAsJsonArray(); configuration.put(key, deserialize(array)); } else {//from www. j ava2 s . c o m throw new IllegalArgumentException( "Configuration parameters must be primitives or arrays of primities only but was " + value); } } return configuration; }
From source file:org.eclipse.smarthome.config.core.ConfigurationDeserializer.java
License:Open Source License
private Object deserialize(JsonArray array) { List<Object> list = new LinkedList<>(); for (JsonElement element : array) { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); list.add(deserialize(primitive)); } else {/* www .j a v a2s. c o m*/ throw new IllegalArgumentException("Multiples must only contain primitives but was " + element); } } return list; }
From source file:org.eclipse.smarthome.storage.json.ConfigurationDeserializer.java
License:Open Source License
@Override public Configuration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Configuration configuration = new Configuration(); JsonObject configurationObject = json.getAsJsonObject(); if (configurationObject.get("properties") != null) { JsonObject propertiesObject = configurationObject.get("properties").getAsJsonObject(); for (Entry<String, JsonElement> entry : propertiesObject.entrySet()) { JsonElement value = entry.getValue(); String key = entry.getKey(); if (value.isJsonPrimitive()) { JsonPrimitive primitive = value.getAsJsonPrimitive(); configuration.put(key, deserialize(primitive)); } else if (value.isJsonArray()) { JsonArray array = value.getAsJsonArray(); configuration.put(key, deserialize(array)); } else { throw new IllegalArgumentException( "Configuration parameters must be primitives or arrays of primities only but was " + value); }//from ww w . j av a2 s . co m } } return configuration; }