List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:org.eclim.Eclim.java
License:Open Source License
public static Object toType(JsonElement json) { // null/*from w w w . j a v a 2 s. c om*/ if (json.isJsonNull()) { return null; // int, double, boolean, String } else if (json.isJsonPrimitive()) { JsonPrimitive prim = json.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isString()) { return prim.getAsString(); } else if (prim.isNumber()) { if (prim.getAsString().indexOf('.') != -1) { return prim.getAsDouble(); } return prim.getAsInt(); } // List } else if (json.isJsonArray()) { ArrayList<Object> type = new ArrayList<Object>(); for (JsonElement element : json.getAsJsonArray()) { type.add(toType(element)); } return type; // Map } else if (json.isJsonObject()) { HashMap<String, Object> type = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { type.put(entry.getKey(), toType(entry.getValue())); } return type; } return null; }
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 ww w .ja v a2 s .c om 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.jgit.diff.EditDeserializer.java
License:Apache License
@Override public Edit deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; }//from ww w. j a va 2 s . c o m if (!json.isJsonArray()) { throw new JsonParseException("Expected array for Edit type"); } final JsonArray o = (JsonArray) json; final int cnt = o.size(); if (cnt < 4 || cnt % 4 != 0) { throw new JsonParseException("Expected array of 4 for Edit type"); } if (4 == cnt) { return new Edit(get(o, 0), get(o, 1), get(o, 2), get(o, 3)); } List<Edit> l = new ArrayList<>((cnt / 4) - 1); for (int i = 4; i < cnt;) { int as = get(o, i++); int ae = get(o, i++); int bs = get(o, i++); int be = get(o, i++); l.add(new Edit(as, ae, bs, be)); } return new ReplaceEdit(get(o, 0), get(o, 1), get(o, 2), get(o, 3), l); }
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(); }// w w w. java 2 s. c om 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.j a va 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 . c om 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.binding.tradfri.handler.TradfriControllerHandler.java
License:Open Source License
@Override public void onUpdate(JsonElement data) { if (active && !(data.isJsonNull())) { state = new TradfriControllerData(data); updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE); DecimalType batteryLevel = state.getBatteryLevel(); if (batteryLevel != null) { updateState(CHANNEL_BATTERY_LEVEL, batteryLevel); }// w w w.ja v a2s . c o m OnOffType batteryLow = state.getBatteryLow(); if (batteryLow != null) { updateState(CHANNEL_BATTERY_LOW, batteryLow); } updateDeviceProperties(state); logger.debug( "Updating thing for controllerId {} to state {batteryLevel: {}, batteryLow: {}, firmwareVersion: {}, modelId: {}, vendor: {}}", state.getDeviceId(), batteryLevel, batteryLow, state.getFirmwareVersion(), state.getModelId(), state.getVendor()); } }
From source file:org.eclipse.smarthome.binding.tradfri.handler.TradfriLightHandler.java
License:Open Source License
@Override public void onUpdate(JsonElement data) { if (active && !(data.isJsonNull())) { state = new LightData(data); updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE); if (!state.getOnOffState()) { logger.debug("Setting state to OFF"); updateState(CHANNEL_BRIGHTNESS, PercentType.ZERO); // if we are turned off, we do not set any brightness value return; }/*from w w w.ja v a2 s. c o m*/ PercentType dimmer = state.getBrightness(); if (dimmer != null) { logger.debug("Updating brightness to {}", dimmer); updateState(CHANNEL_BRIGHTNESS, dimmer); } PercentType colorTemp = state.getColorTemperature(); if (colorTemp != null) { logger.debug("Updating color temperature to {} ", colorTemp); updateState(CHANNEL_COLOR_TEMPERATURE, colorTemp); } } }
From source file:org.eclipse.smarthome.binding.tradfri.handler.TradfriPlugHandler.java
License:Open Source License
@Override public void onUpdate(JsonElement data) { if (active && !(data.isJsonNull())) { TradfriPlugData state = new TradfriPlugData(data); updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE); updateState(CHANNEL_POWER, state.getOnOffState() ? OnOffType.ON : OnOffType.OFF); updateDeviceProperties(state);/*from w w w. j a v a 2s.c om*/ } }
From source file:org.eclipse.smarthome.binding.tradfri.handler.TradfriSensorHandler.java
License:Open Source License
@Override public void onUpdate(JsonElement data) { if (active && !(data.isJsonNull())) { state = new TradfriSensorData(data); updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE); DecimalType batteryLevel = state.getBatteryLevel(); if (batteryLevel != null) { updateState(CHANNEL_BATTERY_LEVEL, batteryLevel); }/* w w w.j a v a2 s. c om*/ OnOffType batteryLow = state.getBatteryLow(); if (batteryLow != null) { updateState(CHANNEL_BATTERY_LOW, batteryLow); } updateDeviceProperties(state); logger.debug( "Updating thing for sensorId {} to state {batteryLevel: {}, batteryLow: {}, firmwareVersion: {}, modelId: {}, vendor: {}}", state.getDeviceId(), batteryLevel, batteryLow, state.getFirmwareVersion(), state.getModelId(), state.getVendor()); } }