use of org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto in project thingsboard by thingsboard.
the class JsonConverter method buildNumericKeyValueProto.
private static KeyValueProto buildNumericKeyValueProto(JsonPrimitive value, String key) {
String valueAsString = value.getAsString();
KeyValueProto.Builder builder = KeyValueProto.newBuilder().setKey(key);
var bd = new BigDecimal(valueAsString);
if (bd.stripTrailingZeros().scale() <= 0 && !isSimpleDouble(valueAsString)) {
try {
return builder.setType(KeyValueType.LONG_V).setLongV(bd.longValueExact()).build();
} catch (ArithmeticException e) {
if (isTypeCastEnabled) {
return builder.setType(KeyValueType.STRING_V).setStringV(bd.toPlainString()).build();
} else {
throw new JsonSyntaxException("Big integer values are not supported!");
}
}
} else {
if (bd.scale() <= 16) {
return builder.setType(KeyValueType.DOUBLE_V).setDoubleV(bd.doubleValue()).build();
} else if (isTypeCastEnabled) {
return builder.setType(KeyValueType.STRING_V).setStringV(bd.toPlainString()).build();
} else {
throw new JsonSyntaxException("Big integer values are not supported!");
}
}
}
Aggregations