org.bson.BsonDecimal128 Java Examples

The following examples show how to use org.bson.BsonDecimal128. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DecimalFieldConverter.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public BsonValue toBson(final 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);
}
 
Example #2
Source File: BsonDecimal128Test.java    From immutables with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: DecimalFieldConverter.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example #4
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void decimal128() throws IOException {
  BsonDecimal128 value = new BsonDecimal128(new Decimal128(BigDecimal.valueOf(1.1)));
  check(Parsers.parserAt(value).getCurrentToken()).is(JsonToken.VALUE_NUMBER_FLOAT);
  check(Parsers.parserAt(value).getNumberType()).is(JsonParser.NumberType.BIG_DECIMAL);
  check(Parsers.parserAt(value).getDoubleValue()).is(1.1);
  check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(1.1));
}
 
Example #5
Source File: SinkFieldConverterTest.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@TestFactory
@DisplayName("tests for logical type decimal field conversions (new)")
List<DynamicTest> testDecimalFieldConverterNew() {

  SinkFieldConverter converter = new DecimalFieldConverter();

  List<DynamicTest> tests = new ArrayList<>();
  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;
}
 
Example #6
Source File: WritecontextTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Test
public void parseBsonArrayWithValues() throws IOException {

    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);

    BsonDocument document = new BsonDocument()
            .append("int32", new BsonInt32(12))
            .append("int64", new BsonInt64(77L))
            .append("bo\"olean", new BsonBoolean(true))
            .append("date", new BsonDateTime(new Date().getTime()))
            .append("double", new BsonDouble(12.3))
            .append("string", new BsonString("pinpoint"))
            .append("objectId", new BsonObjectId(new ObjectId()))
            .append("code", new BsonJavaScript("int i = 10;"))
            .append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1))))
            .append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big"))
            .append("symbol", new BsonSymbol("wow"))
            .append("timestamp", new BsonTimestamp(0x12345678, 5))
            .append("undefined", new BsonUndefined())
            .append("binary1", new BsonBinary(new byte[]{(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20}))
            .append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[]{1, 1, 1, 1, 1}))
            .append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7))))
            .append("document", new BsonDocument("a", new BsonInt32(77)))
            .append("dbPointer", new BsonDbPointer("db.coll", new ObjectId()))
            .append("null", new BsonNull())
            .append("decimal128", new BsonDecimal128(new Decimal128(55)));

    BasicDBObject query = new BasicDBObject();
    query.put("ComplexBson", document);

    logger.debug("document:{}", document);

    NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[]{query}, true);
    logger.debug("val:{}", stringStringValue);

    List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
    Assert.assertEquals(list.size(), 1);
    Map<String, ?> query1Map = (Map<String, ?>) list.get(0);

    checkValue(query1Map);
}