List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException
public JsonSyntaxException(Throwable cause)
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static List<KeyValueProto> parseProtoValues(JsonObject valuesObject) { List<KeyValueProto> result = new ArrayList<>(); for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) { JsonElement element = valueEntry.getValue(); if (element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isString()) { if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) { String message = String.format( "String value length [%d] for key [%s] is greater than maximum allowed [%d]", value.getAsString().length(), valueEntry.getKey(), maxStringValueLength); throw new JsonSyntaxException(message); }/* www.ja v a2 s.c om*/ if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { try { result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); } catch (RuntimeException th) { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build()); } } else { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build()); } } else if (value.isBoolean()) { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.BOOLEAN_V).setBoolV(value.getAsBoolean()).build()); } else if (value.isNumber()) { result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); } } return result; }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static KeyValueProto buildNumericKeyValueProto(JsonPrimitive value, String key) { if (value.getAsString().contains(".")) { return KeyValueProto.newBuilder().setKey(key).setType(KeyValueType.DOUBLE_V) .setDoubleV(value.getAsDouble()).build(); } else {//w w w . jav a 2 s. c o m try { long longValue = Long.parseLong(value.getAsString()); return KeyValueProto.newBuilder().setKey(key).setType(KeyValueType.LONG_V).setLongV(longValue) .build(); } catch (NumberFormatException e) { throw new JsonSyntaxException("Big integer values are not supported!"); } } }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static void parseNumericValue(List<KvEntry> result, Entry<String, JsonElement> valueEntry, JsonPrimitive value) {//from w ww . j av a 2 s . co m if (value.getAsString().contains(".")) { result.add(new DoubleDataEntry(valueEntry.getKey(), value.getAsDouble())); } else { try { long longValue = Long.parseLong(value.getAsString()); result.add(new LongDataEntry(valueEntry.getKey(), longValue)); } catch (NumberFormatException e) { throw new JsonSyntaxException("Big integer values are not supported!"); } } }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static List<KvEntry> parseValues(JsonObject valuesObject) { List<KvEntry> result = new ArrayList<>(); for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) { JsonElement element = valueEntry.getValue(); if (element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isString()) { if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) { String message = String.format( "String value length [%d] for key [%s] is greater than maximum allowed [%d]", value.getAsString().length(), valueEntry.getKey(), maxStringValueLength); throw new JsonSyntaxException(message); }//from w ww. j a va 2 s. c o m if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { try { parseNumericValue(result, valueEntry, value); } catch (RuntimeException th) { result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); } } else { result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); } } else if (value.isBoolean()) { result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean())); } else if (value.isNumber()) { parseNumericValue(result, valueEntry, value); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); } } return result; }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
public static Map<Long, List<KvEntry>> convertToTelemetry(JsonElement jsonObject, long systemTs) throws JsonSyntaxException { Map<Long, List<KvEntry>> result = new HashMap<>(); if (jsonObject.isJsonObject()) { parseObject(result, systemTs, jsonObject); } else if (jsonObject.isJsonArray()) { jsonObject.getAsJsonArray().forEach(je -> { if (je.isJsonObject()) { parseObject(result, systemTs, je.getAsJsonObject()); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + je); }// w ww. j a v a 2 s . com }); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + jsonObject); } return result; }
From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionCtx.java
License:Apache License
public void onDeviceTelemetry(MqttPublishMessage mqttMsg) throws AdaptorException { JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload()); int requestId = mqttMsg.variableHeader().messageId(); if (json.isJsonObject()) { JsonObject jsonObj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) { String deviceName = checkDeviceConnected(deviceEntry.getKey()); if (!deviceEntry.getValue().isJsonArray()) { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); }//from ww w .ja v a2 s.co m BasicTelemetryUploadRequest request = new BasicTelemetryUploadRequest(requestId); JsonArray deviceData = deviceEntry.getValue().getAsJsonArray(); for (JsonElement element : deviceData) { JsonConverter.parseWithTs(request, element.getAsJsonObject()); } GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName); processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request))); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); } }
From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionCtx.java
License:Apache License
public void onDeviceRpcResponse(MqttPublishMessage mqttMsg) throws AdaptorException { JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload()); if (json.isJsonObject()) { JsonObject jsonObj = json.getAsJsonObject(); String deviceName = checkDeviceConnected(jsonObj.get(DEVICE_PROPERTY).getAsString()); Integer requestId = jsonObj.get("id").getAsInt(); String data = jsonObj.get("data").toString(); GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName); processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, new ToDeviceRpcResponseMsg(requestId, data)))); } else {/*from www .ja v a 2s.co m*/ throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); } }
From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionCtx.java
License:Apache License
public void onDeviceAttributes(MqttPublishMessage mqttMsg) throws AdaptorException { JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload()); int requestId = mqttMsg.variableHeader().messageId(); if (json.isJsonObject()) { JsonObject jsonObj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) { String deviceName = checkDeviceConnected(deviceEntry.getKey()); if (!deviceEntry.getValue().isJsonObject()) { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); }/*w w w. j a v a 2s . c o m*/ long ts = System.currentTimeMillis(); BasicUpdateAttributesRequest request = new BasicUpdateAttributesRequest(requestId); JsonObject deviceData = deviceEntry.getValue().getAsJsonObject(); request.add(JsonConverter.parseValues(deviceData).stream() .map(kv -> new BaseAttributeKvEntry(kv, ts)).collect(Collectors.toList())); GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName); processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request))); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); } }
From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionCtx.java
License:Apache License
public void onDeviceAttributesRequest(MqttPublishMessage msg) throws AdaptorException { JsonElement json = validateJsonPayload(gatewaySessionId, msg.payload()); if (json.isJsonObject()) { JsonObject jsonObj = json.getAsJsonObject(); int requestId = jsonObj.get("id").getAsInt(); String deviceName = jsonObj.get(DEVICE_PROPERTY).getAsString(); boolean clientScope = jsonObj.get("client").getAsBoolean(); Set<String> keys; if (jsonObj.has("key")) { keys = Collections.singleton(jsonObj.get("key").getAsString()); } else {// w ww.java 2 s.co m JsonArray keysArray = jsonObj.get("keys").getAsJsonArray(); keys = new HashSet<>(); for (JsonElement keyObj : keysArray) { keys.add(keyObj.getAsString()); } } BasicGetAttributesRequest request; if (clientScope) { request = new BasicGetAttributesRequest(requestId, keys, null); } else { request = new BasicGetAttributesRequest(requestId, null, keys); } GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName); processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, request))); ack(msg); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); } }
From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler.java
License:Apache License
public void onDeviceTelemetry(MqttPublishMessage mqttMsg) throws AdaptorException { JsonElement json = JsonMqttAdaptor.validateJsonPayload(sessionId, mqttMsg.payload()); int msgId = mqttMsg.variableHeader().packetId(); if (json.isJsonObject()) { JsonObject jsonObj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) { String deviceName = deviceEntry.getKey(); Futures.addCallback(checkDeviceConnected(deviceName), new FutureCallback<GatewayDeviceSessionCtx>() { @Override public void onSuccess(@Nullable GatewayDeviceSessionCtx deviceCtx) { if (!deviceEntry.getValue().isJsonArray()) { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); }//from w w w. j av a 2 s .c om TransportProtos.PostTelemetryMsg postTelemetryMsg = JsonConverter .convertToTelemetryProto(deviceEntry.getValue().getAsJsonArray()); transportService.process(deviceCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(channel, deviceName, msgId, postTelemetryMsg)); } @Override public void onFailure(Throwable t) { log.debug("[{}] Failed to process device teleemtry command: {}", sessionId, deviceName, t); } }, context.getExecutor()); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json); } }