use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_decimal128.
@Test
public void createObjectFromJson_decimal128() throws JSONException {
JSONObject json = new JSONObject();
json.put("columnDecimal128", new Decimal128(BigDecimal.TEN));
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, json);
realm.commitTransaction();
AllTypes obj = realm.where(AllTypes.class).findFirst();
assertEquals(new Decimal128(BigDecimal.TEN), obj.getColumnDecimal128());
}
use of org.bson.types.Decimal128 in project morphia by mongodb.
the class TypeExpressionsTest method testToDecimal.
@Test
public void testToDecimal() {
checkMinServerVersion(4.0);
assertAndCheckDocShape("{$toDecimal: true }", toDecimal(value(true)), new Decimal128(1));
}
use of org.bson.types.Decimal128 in project pinpoint by pinpoint-apm.
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 chunjun by DTStack.
the class MongodbRowConverter method createMongoInternalConverter.
private MongoDeserializationConverter createMongoInternalConverter(LogicalType type) {
switch(type.getTypeRoot()) {
case NULL:
return val -> null;
case BOOLEAN:
case FLOAT:
case DOUBLE:
case INTERVAL_YEAR_MONTH:
case INTERVAL_DAY_TIME:
case INTEGER:
case BIGINT:
return val -> val;
case TINYINT:
return val -> ((Integer) val).byteValue();
case SMALLINT:
// JDBC 1.0 use int type for small values.
return val -> val instanceof Integer ? ((Integer) val).shortValue() : val;
case DECIMAL:
final int precision = ((DecimalType) type).getPrecision();
final int scale = ((DecimalType) type).getScale();
return val -> DecimalData.fromBigDecimal(((Decimal128) val).bigDecimalValue(), precision, scale);
case DATE:
return val -> {
Date date = new Date(((java.util.Date) val).getTime());
return (int) date.toLocalDate().toEpochDay();
};
case TIME_WITHOUT_TIME_ZONE:
return val -> (int) ((Time.valueOf(String.valueOf(val))).toLocalTime().toNanoOfDay() / 1_000_000L);
case TIMESTAMP_WITH_TIME_ZONE:
case TIMESTAMP_WITHOUT_TIME_ZONE:
return val -> {
if (val instanceof java.util.Date) {
return TimestampData.fromEpochMillis(((java.util.Date) val).getTime());
}
return TimestampData.fromTimestamp((Timestamp) val);
};
case CHAR:
case VARCHAR:
return val -> StringData.fromString(val.toString());
case BINARY:
case VARBINARY:
return val -> ((Binary) val).getData();
case ARRAY:
case ROW:
case MAP:
case MULTISET:
case RAW:
default:
throw new UnsupportedOperationException("Unsupported type:" + type);
}
}
use of org.bson.types.Decimal128 in project micronaut-serialization by micronaut-projects.
the class BsonRepresentationSerde method doSerialize.
@Override
protected void doSerialize(BsonWriterEncoder encoder, EncoderContext context, Object value, Argument<?> type) throws IOException {
if (value == null) {
encoder.encodeNull();
} else {
BsonWriter bsonWriter = encoder.getBsonWriter();
BsonType bsonType = getBsonType(type);
switch(bsonType) {
case DOUBLE:
bsonWriter.writeDouble(convert(context, value, Double.class));
break;
case STRING:
if (value instanceof ObjectId) {
bsonWriter.writeString(((ObjectId) value).toHexString());
} else {
bsonWriter.writeString(convert(context, value, String.class));
}
break;
case BINARY:
if (value instanceof byte[]) {
bsonWriter.writeBinaryData(new BsonBinary((byte[]) value));
} else if (value instanceof UUID) {
bsonWriter.writeBinaryData(new BsonBinary((UUID) value));
} else {
bsonWriter.writeBinaryData(convert(context, value, BsonBinary.class));
}
break;
case OBJECT_ID:
if (value instanceof String) {
bsonWriter.writeObjectId(new ObjectId((String) value));
} else {
bsonWriter.writeObjectId(convert(context, value, ObjectId.class));
}
break;
case BOOLEAN:
bsonWriter.writeBoolean(convert(context, value, Boolean.class));
break;
case DATE_TIME:
if (value instanceof Long) {
bsonWriter.writeDateTime((Long) value);
} else {
bsonWriter.writeDateTime(convert(context, value, Instant.class).getEpochSecond());
}
break;
case REGULAR_EXPRESSION:
bsonWriter.writeRegularExpression(convert(context, value, BsonRegularExpression.class));
break;
case DB_POINTER:
bsonWriter.writeDBPointer(convert(context, value, BsonDbPointer.class));
break;
case JAVASCRIPT:
bsonWriter.writeJavaScript(convert(context, value, String.class));
break;
case SYMBOL:
bsonWriter.writeSymbol(convert(context, value, String.class));
break;
case JAVASCRIPT_WITH_SCOPE:
bsonWriter.writeJavaScriptWithScope(convert(context, value, String.class));
break;
case INT32:
bsonWriter.writeInt32(convert(context, value, Integer.class));
break;
case TIMESTAMP:
bsonWriter.writeTimestamp(convert(context, value, BsonTimestamp.class));
break;
case INT64:
bsonWriter.writeInt64(convert(context, value, Long.class));
break;
case DECIMAL128:
if (value instanceof BigDecimal) {
bsonWriter.writeDecimal128(new Decimal128((BigDecimal) value));
} else {
bsonWriter.writeDecimal128(convert(context, value, Decimal128.class));
}
break;
default:
throw new SerdeException("Unsupported BsonType: " + bsonType);
}
}
}
Aggregations