Commit dcba4f6b582d1902740d506d92e178bb8c48544f
Merge branch 'master' of github.com:thingsboard/thingsboard
Showing
1 changed file
with
40 additions
and
15 deletions
... | ... | @@ -22,6 +22,7 @@ import com.google.gson.JsonObject; |
22 | 22 | import com.google.gson.JsonParser; |
23 | 23 | import com.google.gson.JsonPrimitive; |
24 | 24 | import com.google.gson.JsonSyntaxException; |
25 | +import org.apache.commons.lang3.math.NumberUtils; | |
25 | 26 | import org.springframework.util.StringUtils; |
26 | 27 | import org.thingsboard.server.common.data.kv.AttributeKey; |
27 | 28 | import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
... | ... | @@ -128,24 +129,22 @@ public class JsonConverter { |
128 | 129 | if (element.isJsonPrimitive()) { |
129 | 130 | JsonPrimitive value = element.getAsJsonPrimitive(); |
130 | 131 | if (value.isString()) { |
131 | - result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) | |
132 | - .setStringV(value.getAsString()).build()); | |
132 | + if(NumberUtils.isParsable(value.getAsString())) { | |
133 | + try { | |
134 | + result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); | |
135 | + } catch (Throwable th) { | |
136 | + result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) | |
137 | + .setStringV(value.getAsString()).build()); | |
138 | + } | |
139 | + } else { | |
140 | + result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V) | |
141 | + .setStringV(value.getAsString()).build()); | |
142 | + } | |
133 | 143 | } else if (value.isBoolean()) { |
134 | 144 | result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.BOOLEAN_V) |
135 | 145 | .setBoolV(value.getAsBoolean()).build()); |
136 | 146 | } else if (value.isNumber()) { |
137 | - if (value.getAsString().contains(".")) { | |
138 | - result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.DOUBLE_V) | |
139 | - .setDoubleV(value.getAsDouble()).build()); | |
140 | - } else { | |
141 | - try { | |
142 | - long longValue = Long.parseLong(value.getAsString()); | |
143 | - result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.LONG_V) | |
144 | - .setLongV(longValue).build()); | |
145 | - } catch (NumberFormatException e) { | |
146 | - throw new JsonSyntaxException("Big integer values are not supported!"); | |
147 | - } | |
148 | - } | |
147 | + result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); | |
149 | 148 | } else { |
150 | 149 | throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); |
151 | 150 | } |
... | ... | @@ -156,6 +155,24 @@ public class JsonConverter { |
156 | 155 | return result; |
157 | 156 | } |
158 | 157 | |
158 | + private static KeyValueProto buildNumericKeyValueProto(JsonPrimitive value, String key) { | |
159 | + if (value.getAsString().contains(".")) { | |
160 | + return KeyValueProto.newBuilder() | |
161 | + .setKey(key) | |
162 | + .setType(KeyValueType.DOUBLE_V) | |
163 | + .setDoubleV(value.getAsDouble()) | |
164 | + .build(); | |
165 | + } else { | |
166 | + try { | |
167 | + long longValue = Long.parseLong(value.getAsString()); | |
168 | + return KeyValueProto.newBuilder().setKey(key).setType(KeyValueType.LONG_V) | |
169 | + .setLongV(longValue).build(); | |
170 | + } catch (NumberFormatException e) { | |
171 | + throw new JsonSyntaxException("Big integer values are not supported!"); | |
172 | + } | |
173 | + } | |
174 | + } | |
175 | + | |
159 | 176 | public static TransportProtos.ToServerRpcRequestMsg convertToServerRpcRequest(JsonElement json, int requestId) throws JsonSyntaxException { |
160 | 177 | JsonObject object = json.getAsJsonObject(); |
161 | 178 | return TransportProtos.ToServerRpcRequestMsg.newBuilder().setRequestId(requestId).setMethodName(object.get("method").getAsString()).setParams(GSON.toJson(object.get("params"))).build(); |
... | ... | @@ -370,7 +387,15 @@ public class JsonConverter { |
370 | 387 | if (element.isJsonPrimitive()) { |
371 | 388 | JsonPrimitive value = element.getAsJsonPrimitive(); |
372 | 389 | if (value.isString()) { |
373 | - result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); | |
390 | + if(NumberUtils.isParsable(value.getAsString())) { | |
391 | + try { | |
392 | + parseNumericValue(result, valueEntry, value); | |
393 | + } catch (Throwable th) { | |
394 | + result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); | |
395 | + } | |
396 | + } else { | |
397 | + result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); | |
398 | + } | |
374 | 399 | } else if (value.isBoolean()) { |
375 | 400 | result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean())); |
376 | 401 | } else if (value.isNumber()) { | ... | ... |