Commit d51b76fd476ec90d697d5b232e51750884e09689

Authored by vparomskiy
1 parent f6b00b35

enable/disable type cast for telemetry/attributes json

... ... @@ -443,3 +443,7 @@ transport:
443 443 bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
444 444 bind_port: "${COAP_BIND_PORT:5683}"
445 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}"
\ No newline at end of file
... ...
... ... @@ -59,6 +59,8 @@ public class JsonConverter {
59 59 private static final String CAN_T_PARSE_VALUE = "Can't parse value: ";
60 60 private static final String DEVICE_PROPERTY = "device";
61 61
  62 + private static boolean isTypeCastEnabled = true;
  63 +
62 64 public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException {
63 65 long systemTs = System.currentTimeMillis();
64 66 PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder();
... ... @@ -129,10 +131,10 @@ public class JsonConverter {
129 131 if (element.isJsonPrimitive()) {
130 132 JsonPrimitive value = element.getAsJsonPrimitive();
131 133 if (value.isString()) {
132   - if(NumberUtils.isParsable(value.getAsString())) {
  134 + if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
133 135 try {
134 136 result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
135   - } catch (Throwable th) {
  137 + } catch (RuntimeException th) {
136 138 result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V)
137 139 .setStringV(value.getAsString()).build());
138 140 }
... ... @@ -387,10 +389,10 @@ public class JsonConverter {
387 389 if (element.isJsonPrimitive()) {
388 390 JsonPrimitive value = element.getAsJsonPrimitive();
389 391 if (value.isString()) {
390   - if(NumberUtils.isParsable(value.getAsString())) {
  392 + if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
391 393 try {
392 394 parseNumericValue(result, valueEntry, value);
393   - } catch (Throwable th) {
  395 + } catch (RuntimeException th) {
394 396 result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
395 397 }
396 398 } else {
... ... @@ -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 52 topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
53 53 poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
54 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}"
\ No newline at end of file
... ...
... ... @@ -53,3 +53,7 @@ kafka:
53 53 topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
54 54 poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
55 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}"
\ No newline at end of file
... ...
... ... @@ -72,3 +72,7 @@ kafka:
72 72 topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
73 73 poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
74 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}"
\ No newline at end of file
... ...