Browse Source

enable/disable type cast for telemetry/attributes json

pull/1215/head
vparomskiy 8 years ago
parent
commit
d51b76fd47
  1. 4
      application/src/main/resources/thingsboard.yml
  2. 14
      common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java
  3. 16
      common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverterConfig.java
  4. 4
      transport/coap/src/main/resources/tb-coap-transport.yml
  5. 4
      transport/http/src/main/resources/tb-http-transport.yml
  6. 4
      transport/mqtt/src/main/resources/tb-mqtt-transport.yml

4
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}"

14
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;
}
}

16
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);
}
}

4
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}"

4
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}"

4
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}"
Loading…
Cancel
Save