Commit d51b76fd476ec90d697d5b232e51750884e09689
1 parent
f6b00b35
enable/disable type cast for telemetry/attributes json
Showing
6 changed files
with
41 additions
and
5 deletions
@@ -443,3 +443,7 @@ transport: | @@ -443,3 +443,7 @@ transport: | ||
443 | bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}" | 443 | bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}" |
444 | bind_port: "${COAP_BIND_PORT:5683}" | 444 | bind_port: "${COAP_BIND_PORT:5683}" |
445 | timeout: "${COAP_TIMEOUT:10000}" | 445 | timeout: "${COAP_TIMEOUT:10000}" |
446 | + | ||
447 | +json: | ||
448 | + # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON | ||
449 | + type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:false}" |
@@ -59,6 +59,8 @@ public class JsonConverter { | @@ -59,6 +59,8 @@ public class JsonConverter { | ||
59 | private static final String CAN_T_PARSE_VALUE = "Can't parse value: "; | 59 | private static final String CAN_T_PARSE_VALUE = "Can't parse value: "; |
60 | private static final String DEVICE_PROPERTY = "device"; | 60 | private static final String DEVICE_PROPERTY = "device"; |
61 | 61 | ||
62 | + private static boolean isTypeCastEnabled = true; | ||
63 | + | ||
62 | public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException { | 64 | public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException { |
63 | long systemTs = System.currentTimeMillis(); | 65 | long systemTs = System.currentTimeMillis(); |
64 | PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder(); | 66 | PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder(); |
@@ -129,10 +131,10 @@ public class JsonConverter { | @@ -129,10 +131,10 @@ public class JsonConverter { | ||
129 | if (element.isJsonPrimitive()) { | 131 | if (element.isJsonPrimitive()) { |
130 | JsonPrimitive value = element.getAsJsonPrimitive(); | 132 | JsonPrimitive value = element.getAsJsonPrimitive(); |
131 | if (value.isString()) { | 133 | if (value.isString()) { |
132 | - if(NumberUtils.isParsable(value.getAsString())) { | 134 | + if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { |
133 | try { | 135 | try { |
134 | result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); | 136 | result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); |
135 | - } catch (Throwable th) { | 137 | + } catch (RuntimeException th) { |
136 | result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) | 138 | result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) |
137 | .setStringV(value.getAsString()).build()); | 139 | .setStringV(value.getAsString()).build()); |
138 | } | 140 | } |
@@ -387,10 +389,10 @@ public class JsonConverter { | @@ -387,10 +389,10 @@ public class JsonConverter { | ||
387 | if (element.isJsonPrimitive()) { | 389 | if (element.isJsonPrimitive()) { |
388 | JsonPrimitive value = element.getAsJsonPrimitive(); | 390 | JsonPrimitive value = element.getAsJsonPrimitive(); |
389 | if (value.isString()) { | 391 | if (value.isString()) { |
390 | - if(NumberUtils.isParsable(value.getAsString())) { | 392 | + if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { |
391 | try { | 393 | try { |
392 | parseNumericValue(result, valueEntry, value); | 394 | parseNumericValue(result, valueEntry, value); |
393 | - } catch (Throwable th) { | 395 | + } catch (RuntimeException th) { |
394 | result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); | 396 | result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); |
395 | } | 397 | } |
396 | } else { | 398 | } else { |
@@ -451,5 +453,7 @@ public class JsonConverter { | @@ -451,5 +453,7 @@ public class JsonConverter { | ||
451 | } | 453 | } |
452 | } | 454 | } |
453 | 455 | ||
454 | - | 456 | + public static void setTypeCastEnabled(boolean enabled) { |
457 | + isTypeCastEnabled = enabled; | ||
458 | + } | ||
455 | } | 459 | } |
1 | +package org.thingsboard.server.common.transport.adaptor; | ||
2 | + | ||
3 | +import lombok.extern.slf4j.Slf4j; | ||
4 | +import org.springframework.beans.factory.annotation.Value; | ||
5 | +import org.springframework.context.annotation.Configuration; | ||
6 | + | ||
7 | +@Configuration | ||
8 | +@Slf4j | ||
9 | +public class JsonConverterConfig { | ||
10 | + | ||
11 | + @Value("${json.type_cast_enabled}") | ||
12 | + public void setIsJsonTypeCastEnabled(boolean jsonTypeCastEnabled) { | ||
13 | + JsonConverter.setTypeCastEnabled(jsonTypeCastEnabled); | ||
14 | + log.info("JSON type cast enabled = {}", jsonTypeCastEnabled); | ||
15 | + } | ||
16 | +} |
@@ -52,3 +52,7 @@ kafka: | @@ -52,3 +52,7 @@ kafka: | ||
52 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" | 52 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" |
53 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" | 53 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" |
54 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" | 54 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" |
55 | + | ||
56 | +json: | ||
57 | + # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON | ||
58 | + type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}" |
@@ -53,3 +53,7 @@ kafka: | @@ -53,3 +53,7 @@ kafka: | ||
53 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" | 53 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" |
54 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" | 54 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" |
55 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" | 55 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" |
56 | + | ||
57 | +json: | ||
58 | + # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON | ||
59 | + type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}" |
@@ -72,3 +72,7 @@ kafka: | @@ -72,3 +72,7 @@ kafka: | ||
72 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" | 72 | topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" |
73 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" | 73 | poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" |
74 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" | 74 | auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" |
75 | + | ||
76 | +json: | ||
77 | + # Cast String data types to Numeric if possible when processing Telemetry/Attributes JSON | ||
78 | + type_cast_enabled: "${JSON_TYPE_CAST_ENABLED:true}" |