@Override protected void doWriteDecimal128(final Decimal128 value) { write(new BsonDecimal128(value)); }
@Override public Decimal128 doReadDecimal128() { return currentValue.asDecimal128().getValue(); }
@Test public void write() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument doc = Jsons.toBson(obj); check(doc.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(doc.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); }
return value.asString().getValue(); case DECIMAL128: return value.asDecimal128().doubleValue(); case DOUBLE: return value.asDouble().getValue();
@Test public void bigNumbers() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument bson = Jsons.toBson(obj); check(bson.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(bson.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); check(Jsons.toGson(bson)).is(obj); } }
return value.asString().getValue(); case DECIMAL128: return value.asDecimal128().doubleValue(); case DOUBLE: return value.asDouble().getValue();
@Override public BsonDecimal128 decode(final BsonReader reader, final DecoderContext decoderContext) { return new BsonDecimal128(reader.readDecimal128()); }
@Override public void encode(final BsonWriter writer, final BsonDecimal128 value, final EncoderContext encoderContext) { writer.writeDecimal128(value.getValue()); }
value.asDecimal128().decimal128Value().getHigh(), value.asDecimal128().decimal128Value().getLow()); case JAVASCRIPT_WITH_SCOPE: { BsonJavaScriptWithScope casted = value.asJavaScriptWithScope();
@Test public void read() throws Exception { BsonDocument doc = new BsonDocument(); doc.put("int", new BsonDecimal128(Decimal128.parse(Integer.toString(Integer.MAX_VALUE)))); doc.put("long", new BsonDecimal128(new Decimal128(Long.MAX_VALUE))); doc.put("double", new BsonDecimal128(Decimal128.parse("12.111"))); JsonReader reader = Jsons.asGsonReader(doc); reader.beginObject(); check(reader.nextName()).is("int"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextInt()).is(Integer.MAX_VALUE); check(reader.nextName()).is("long"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextLong()).is(Long.MAX_VALUE); check(reader.nextName()).is("double"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextDouble()).is(12.111D); reader.endObject(); reader.close(); }
colValue = keyvalueforStruct.getValue().asDecimal128().getValue().toString(); break; list.add(temp); } else if (arrValue.getBsonType() == BsonType.DECIMAL128 && valueType == BsonType.DECIMAL128) { String temp = arrValue.asDecimal128().getValue().toString(); list.add(temp); } else if (arrValue.getBsonType() == BsonType.TIMESTAMP && valueType == BsonType.TIMESTAMP) {
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
break; case DECIMAL128: writeDecimal128(value.asDecimal128().getValue()); break; case MIN_KEY:
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
public static Object extractValueEx(BsonValue value) { switch (value.getBsonType()) { case DOUBLE: return value.asDouble().getValue(); case STRING: return value.asString().getValue(); case OBJECT_ID: return value.asObjectId().getValue(); case INT32: return value.asInt32().getValue(); case INT64: return value.asInt64().getValue(); case DECIMAL128: return value.asDecimal128().getValue(); case NULL: return null; } throw new IllegalArgumentException("Unsupported ID type: " + value.getClass()); }
private BsonValue constructValue(Object value) { if (value == null) { return BsonNull.VALUE; } else if (value instanceof Double) { return new BsonDouble((Double) value); } else if (value instanceof String) { return new BsonString((String) value); } else if (value instanceof ObjectId) { return new BsonObjectId((ObjectId) value); } else if (value instanceof Integer) { return new BsonInt32((Integer) value); } else if (value instanceof Long) { return new BsonInt64((Long) value); } else if (value instanceof Decimal128) { return new BsonDecimal128((Decimal128) value); } throw new IllegalArgumentException("Unsupported ID type: " + value.getClass()); }
@TestFactory @DisplayName("tests for logical type decimal field conversions (new)") public List<DynamicTest> testDecimalFieldConverterNew() { SinkFieldConverter converter = new DecimalFieldConverter(); List<DynamicTest> tests = new ArrayList<>(); new ArrayList<>(Arrays.asList( new BigDecimal("-1234567890.09876543210"), BigDecimal.ZERO, new BigDecimal("+1234567890.09876543210") )).forEach( el -> tests.add(dynamicTest("conversion with " + converter.getClass().getSimpleName() + " for "+el, () -> assertEquals(el, ((BsonDecimal128)converter.toBson(el)).getValue().bigDecimalValue()) )) ); tests.add(dynamicTest("optional type conversions", () -> { Schema valueOptionalDefault = Decimal.builder(0).optional().defaultValue(BigDecimal.ZERO); assertAll("checks", () -> assertThrows(DataException.class, () -> converter.toBson(null, Decimal.schema(0))), () -> assertEquals(new BsonNull(), converter.toBson(null, Decimal.builder(0).optional())), () -> assertEquals(valueOptionalDefault.defaultValue(), ((BsonDecimal128)converter.toBson(null,valueOptionalDefault)).getValue().bigDecimalValue()) ); })); return tests; }
).toInstant()).getTime() )) .append("myDecimal", new BsonDecimal128(new Decimal128(new BigDecimal("12345.6789"))));
com.torodb.mongowp.bson.BsonDecimal128 casted = (com.torodb.mongowp.bson.BsonDecimal128) value; return new org.bson.BsonDecimal128( Decimal128.fromIEEE754BIDEncoding(casted.getHigh(), casted.getLow()));