enable/disable type cast for telemetry/attributes json

This commit is contained in:
vparomskiy 2018-11-02 15:05:11 +02:00
parent f6b00b35db
commit d51b76fd47
6 changed files with 41 additions and 5 deletions

View File

@ -443,3 +443,7 @@ transport:
bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}" bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
bind_port: "${COAP_BIND_PORT:5683}" bind_port: "${COAP_BIND_PORT:5683}"
timeout: "${COAP_TIMEOUT:10000}" 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}"

View File

@ -59,6 +59,8 @@ public class JsonConverter {
private static final String CAN_T_PARSE_VALUE = "Can't parse value: "; private static final String CAN_T_PARSE_VALUE = "Can't parse value: ";
private static final String DEVICE_PROPERTY = "device"; private static final String DEVICE_PROPERTY = "device";
private static boolean isTypeCastEnabled = true;
public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException { public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException {
long systemTs = System.currentTimeMillis(); long systemTs = System.currentTimeMillis();
PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder(); PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder();
@ -129,10 +131,10 @@ public class JsonConverter {
if (element.isJsonPrimitive()) { if (element.isJsonPrimitive()) {
JsonPrimitive value = element.getAsJsonPrimitive(); JsonPrimitive value = element.getAsJsonPrimitive();
if (value.isString()) { if (value.isString()) {
if(NumberUtils.isParsable(value.getAsString())) { if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
try { try {
result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
} catch (Throwable th) { } catch (RuntimeException th) {
result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V)
.setStringV(value.getAsString()).build()); .setStringV(value.getAsString()).build());
} }
@ -387,10 +389,10 @@ public class JsonConverter {
if (element.isJsonPrimitive()) { if (element.isJsonPrimitive()) {
JsonPrimitive value = element.getAsJsonPrimitive(); JsonPrimitive value = element.getAsJsonPrimitive();
if (value.isString()) { if (value.isString()) {
if(NumberUtils.isParsable(value.getAsString())) { if(isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
try { try {
parseNumericValue(result, valueEntry, value); parseNumericValue(result, valueEntry, value);
} catch (Throwable th) { } catch (RuntimeException th) {
result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
} }
} else { } else {
@ -451,5 +453,7 @@ public class JsonConverter {
} }
} }
public static void setTypeCastEnabled(boolean enabled) {
isTypeCastEnabled = enabled;
}
} }

View File

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

View File

@ -52,3 +52,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" 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}"

View File

@ -53,3 +53,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" 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}"

View File

@ -72,3 +72,7 @@ kafka:
topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}" topic: "${TB_TRANSPORT_NOTIFICATIONS_TOPIC:tb.transport.notifications}"
poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}" poll_interval: "${TB_TRANSPORT_NOTIFICATIONS_POLL_INTERVAL_MS:25}"
auto_commit_interval: "${TB_TRANSPORT_NOTIFICATIONS_AUTO_COMMIT_INTERVAL_MS:100}" 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}"