Commit 633f8b0b63da6276723edfdd16707eabb886d559
Committed by
GitHub
Merge pull request #1215 from vparomskiy/master
enable/disable type cast for telemetry/attributes json
Showing
6 changed files
with
56 additions
and
5 deletions
... | ... | @@ -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 | +/** | |
2 | + * Copyright © 2016-2018 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.common.transport.adaptor; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.springframework.beans.factory.annotation.Value; | |
20 | +import org.springframework.context.annotation.Configuration; | |
21 | + | |
22 | +@Configuration | |
23 | +@Slf4j | |
24 | +public class JsonConverterConfig { | |
25 | + | |
26 | + @Value("${json.type_cast_enabled}") | |
27 | + public void setIsJsonTypeCastEnabled(boolean jsonTypeCastEnabled) { | |
28 | + JsonConverter.setTypeCastEnabled(jsonTypeCastEnabled); | |
29 | + log.info("JSON type cast enabled = {}", jsonTypeCastEnabled); | |
30 | + } | |
31 | +} | ... | ... |
... | ... | @@ -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 | ... | ... |