Search in sources :

Example 1 with KeyValueProto

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!");
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) KeyValueProto(org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto) BigDecimal(java.math.BigDecimal)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BigDecimal (java.math.BigDecimal)1 KeyValueProto (org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto)1