Search in sources :

Example 1 with Decimal128

use of org.bson.types.Decimal128 in project mongo-java-driver by mongodb.

the class BasicBSONEncoder method _putObjectField.

/**
     * Encodes any Object type
     *
     * @param name         the field name
     * @param initialValue the value to write
     */
protected void _putObjectField(final String name, final Object initialValue) {
    if ("_transientFields".equals(name)) {
        return;
    }
    if (name.contains("\0")) {
        throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
    }
    if ("$where".equals(name) && initialValue instanceof String) {
        putCode(name, new Code((String) initialValue));
    }
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        putNull(name);
    } else if (value instanceof Date) {
        putDate(name, (Date) value);
    } else if (value instanceof Number) {
        putNumber(name, (Number) value);
    } else if (value instanceof Decimal128) {
        putDecimal128(name, (Decimal128) value);
    } else if (value instanceof Character) {
        putString(name, value.toString());
    } else if (value instanceof String) {
        putString(name, value.toString());
    } else if (value instanceof ObjectId) {
        putObjectId(name, (ObjectId) value);
    } else if (value instanceof Boolean) {
        putBoolean(name, (Boolean) value);
    } else if (value instanceof Pattern) {
        putPattern(name, (Pattern) value);
    } else if (value instanceof Iterable) {
        putIterable(name, (Iterable) value);
    } else if (value instanceof BSONObject) {
        putObject(name, (BSONObject) value);
    } else if (value instanceof Map) {
        putMap(name, (Map) value);
    } else if (value instanceof byte[]) {
        putBinary(name, (byte[]) value);
    } else if (value instanceof Binary) {
        putBinary(name, (Binary) value);
    } else if (value instanceof UUID) {
        putUUID(name, (UUID) value);
    } else if (value.getClass().isArray()) {
        putArray(name, value);
    } else if (value instanceof Symbol) {
        putSymbol(name, (Symbol) value);
    } else if (value instanceof BSONTimestamp) {
        putTimestamp(name, (BSONTimestamp) value);
    } else if (value instanceof CodeWScope) {
        putCodeWScope(name, (CodeWScope) value);
    } else if (value instanceof Code) {
        putCode(name, (Code) value);
    } else if (value instanceof DBRef) {
        BSONObject temp = new BasicBSONObject();
        DBRef dbRef = (DBRef) value;
        temp.put("$ref", dbRef.getCollectionName());
        temp.put("$id", dbRef.getId());
        if (dbRef.getDatabaseName() != null) {
            temp.put("$db", dbRef.getDatabaseName());
        }
        putObject(name, temp);
    } else if (value instanceof MinKey) {
        putMinKey(name);
    } else if (value instanceof MaxKey) {
        putMaxKey(name);
    } else if (putSpecial(name, value)) {
    // no-op
    } else {
        throw new IllegalArgumentException("Can't serialize " + value.getClass());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 2 with Decimal128

use of org.bson.types.Decimal128 in project mongo-java-driver by mongodb.

the class JSONCallbackTest method numberDecimalParsing.

@Test
public void numberDecimalParsing() {
    Decimal128 number = (Decimal128) JSON.parse(("{\"$numberDecimal\" : \"314E-2\"}"));
    assertEquals(number, Decimal128.parse("314E-2"));
}
Also used : Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 3 with Decimal128

use of org.bson.types.Decimal128 in project debezium by debezium.

the class RecordMakersTest method shouldGenerateRecordsWithCorrectlySerializedId.

@Test
public void shouldGenerateRecordsWithCorrectlySerializedId() throws InterruptedException {
    CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
    BsonTimestamp ts = new BsonTimestamp(1000, 1);
    // long
    Document obj = new Document().append("_id", Long.valueOf(Integer.MAX_VALUE) + 10).append("name", "Sally");
    Document event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    RecordsForCollection records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1002);
    // String
    obj = new Document().append("_id", "123").append("name", "Sally");
    event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1003);
    // Complex key type
    obj = new Document().append("_id", new Document().append("company", 32).append("dept", "home improvement")).append("name", "Sally");
    event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1004);
    // date
    Calendar cal = Calendar.getInstance();
    cal.set(2017, 9, 19);
    obj = new Document().append("_id", cal.getTime()).append("name", "Sally");
    event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1005);
    // Decimal128
    obj = new Document().append("_id", new Decimal128(new BigDecimal("123.45678"))).append("name", "Sally");
    event = new Document().append("o", obj).append("ns", "dbA.c1").append("ts", ts).append("h", Long.valueOf(12345678)).append("op", "i");
    records = recordMakers.forCollection(collectionId);
    records.recordEvent(event, 1004);
    assertThat(produced.size()).isEqualTo(5);
    SourceRecord record = produced.get(0);
    Struct key = (Struct) record.key();
    assertThat(key.get("id")).isEqualTo("2147483657");
    record = produced.get(1);
    key = (Struct) record.key();
    assertThat(key.get("id")).isEqualTo("\"123\"");
    record = produced.get(2);
    key = (Struct) record.key();
    assertThat(key.get("id")).isEqualTo("{ \"company\" : 32 , \"dept\" : \"home improvement\"}");
    record = produced.get(3);
    key = (Struct) record.key();
    // that's actually not what https://docs.mongodb.com/manual/reference/mongodb-extended-json/#date suggests;
    // seems JsonSerializers is not fully compliant with that description
    assertThat(key.get("id")).isEqualTo("{ \"$date\" : " + cal.getTime().getTime() + "}");
    record = produced.get(4);
    key = (Struct) record.key();
    assertThat(key.get("id")).isEqualTo("{ \"$numberDecimal\" : \"123.45678\"}");
}
Also used : RecordsForCollection(io.debezium.connector.mongodb.RecordMakers.RecordsForCollection) Calendar(java.util.Calendar) Decimal128(org.bson.types.Decimal128) Document(org.bson.Document) SourceRecord(org.apache.kafka.connect.source.SourceRecord) BsonTimestamp(org.bson.BsonTimestamp) BigDecimal(java.math.BigDecimal) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 4 with Decimal128

use of org.bson.types.Decimal128 in project pinpoint by naver.

the class WritecontextTest method parseBsonArrayWithValues.

@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);
}
Also used : BsonString(org.bson.BsonString) BsonBoolean(org.bson.BsonBoolean) BasicDBObject(com.mongodb.BasicDBObject) BsonInt32(org.bson.BsonInt32) BsonDecimal128(org.bson.BsonDecimal128) BsonDouble(org.bson.BsonDouble) ArrayList(java.util.ArrayList) List(java.util.List) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope) BsonNull(org.bson.BsonNull) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) BsonDbPointer(org.bson.BsonDbPointer) Decimal128(org.bson.types.Decimal128) BsonDecimal128(org.bson.BsonDecimal128) BsonRegularExpression(org.bson.BsonRegularExpression) BsonObjectId(org.bson.BsonObjectId) BsonJavaScript(org.bson.BsonJavaScript) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) BsonInt64(org.bson.BsonInt64) BsonSymbol(org.bson.BsonSymbol) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) Map(java.util.Map) BsonUndefined(org.bson.BsonUndefined) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 5 with Decimal128

use of org.bson.types.Decimal128 in project mongo-java-driver by mongodb.

the class JsonReader method visitNumberDecimalConstructor.

private Decimal128 visitNumberDecimalConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken valueToken = popToken();
    Decimal128 value;
    if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64 || valueToken.getType() == JsonTokenType.DOUBLE) {
        value = valueToken.getValue(Decimal128.class);
    } else if (valueToken.getType() == JsonTokenType.STRING) {
        value = Decimal128.parse(valueToken.getValue(String.class));
    } else {
        throw new JsonParseException("JSON reader expected a number or a string but found '%s'.", valueToken.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    return value;
}
Also used : Decimal128(org.bson.types.Decimal128)

Aggregations

Decimal128 (org.bson.types.Decimal128)83 ObjectId (org.bson.types.ObjectId)50 Date (java.util.Date)46 Test (org.junit.Test)46 AllTypes (io.realm.entities.AllTypes)22 BigDecimal (java.math.BigDecimal)20 UUID (java.util.UUID)17 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Calendar (java.util.Calendar)6 DefaultValueOfField (io.realm.entities.DefaultValueOfField)5