thingsboard-aplcache
Changes
common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java 14(+9 -5)
Details
diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 51a59c9..cb7beb6 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -443,3 +443,7 @@ transport:
bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
bind_port: "${COAP_BIND_PORT:5683}"
timeout: "${COAP_TIMEOUT:10000}"
+
+json:
+ # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON
+ type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:false}"
\ No newline at end of file
diff --git a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java
index b497fe0..d52a896 100644
--- a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java
+++ b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java
@@ -59,6 +59,8 @@ public class JsonConverter {
private static final String CAN_T_PARSE_VALUE = "Can't parse value: ";
private static final String DEVICE_PROPERTY = "device";
+ private static boolean isTypeCastEnabled = true;
+
public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException {
long systemTs = System.currentTimeMillis();
PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder();
@@ -129,10 +131,10 @@ public class JsonConverter {
if (element.isJsonPrimitive()) {
JsonPrimitive value = element.getAsJsonPrimitive();
if (value.isString()) {
- if(NumberUtils.isParsable(value.getAsString())) {
+ if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
try {
result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
- } catch (Throwable th) {
+ } catch (RuntimeException th) {
result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V)
.setStringV(value.getAsString()).build());
}
@@ -387,10 +389,10 @@ public class JsonConverter {
if (element.isJsonPrimitive()) {
JsonPrimitive value = element.getAsJsonPrimitive();
if (value.isString()) {
- if(NumberUtils.isParsable(value.getAsString())) {
+ if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
try {
parseNumericValue(result, valueEntry, value);
- } catch (Throwable th) {
+ } catch (RuntimeException th) {
result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
}
} else {
@@ -451,5 +453,7 @@ public class JsonConverter {
}
}
-
+ public static void setTypeCastEnabled(boolean enabled) {
+ isTypeCastEnabled = enabled;
+ }
}
diff --git a/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverterConfig.java b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverterConfig.java
new file mode 100644
index 0000000..8b3bda3
--- /dev/null
+++ b/common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverterConfig.java
@@ -0,0 +1,16 @@
+package org.thingsboard.server.common.transport.adaptor;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@Slf4j
+public class JsonConverterConfig {
+
+ @Value("${json.type_cast_enabled}")
+ public void setIsJsonTypeCastEnabled(boolean jsonTypeCastEnabled) {
+ JsonConverter.setTypeCastEnabled(jsonTypeCastEnabled);
+ log.info("JSON type cast enabled = {}", jsonTypeCastEnabled);
+ }
+}
diff --git a/transport/coap/src/main/resources/tb-coap-transport.yml b/transport/coap/src/main/resources/tb-coap-transport.yml
index f96bcbe..d20bc79 100644
--- a/transport/coap/src/main/resources/tb-coap-transport.yml
+++ b/transport/coap/src/main/resources/tb-coap-transport.yml
@@ -52,3 +52,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}"
+
+json:
+ # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON
+ type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}"
\ No newline at end of file
diff --git a/transport/http/src/main/resources/tb-http-transport.yml b/transport/http/src/main/resources/tb-http-transport.yml
index 6d593ed..f5f7855 100644
--- a/transport/http/src/main/resources/tb-http-transport.yml
+++ b/transport/http/src/main/resources/tb-http-transport.yml
@@ -53,3 +53,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}"
+
+json:
+ # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON
+ type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}"
\ No newline at end of file
diff --git a/transport/mqtt/src/main/resources/tb-mqtt-transport.yml b/transport/mqtt/src/main/resources/tb-mqtt-transport.yml
index e7f8942..af1b6f0 100644
--- a/transport/mqtt/src/main/resources/tb-mqtt-transport.yml
+++ b/transport/mqtt/src/main/resources/tb-mqtt-transport.yml
@@ -72,3 +72,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}"
+
+json:
+ # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON
+ type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}"
\ No newline at end of file