enable/disable type cast for telemetry/attributes json
This commit is contained in:
		
							parent
							
								
									f6b00b35db
								
							
						
					
					
						commit
						d51b76fd47
					
				@ -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}"
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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}"
 | 
			
		||||
@ -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}"
 | 
			
		||||
@ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user