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());
}
}
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"));
}
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\"}");
}
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);
}
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;
}
Aggregations