Search in sources :

Example 91 with Decimal128

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());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 92 with Decimal128

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));
}
Also used : Decimal128(org.bson.types.Decimal128) Test(org.testng.annotations.Test)

Example 93 with Decimal128

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);
}
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 94 with Decimal128

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);
    }
}
Also used : Document(org.bson.Document) RowData(org.apache.flink.table.data.RowData) Binary(org.bson.types.Binary) Time(java.sql.Time) TimestampData(org.apache.flink.table.data.TimestampData) Timestamp(java.sql.Timestamp) DecimalData(org.apache.flink.table.data.DecimalData) RowType(org.apache.flink.table.types.logical.RowType) Date(java.sql.Date) ArrayList(java.util.ArrayList) StringData(org.apache.flink.table.data.StringData) TimestampType(org.apache.flink.table.types.logical.TimestampType) List(java.util.List) AbstractRowConverter(com.dtstack.flinkx.converter.AbstractRowConverter) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) ObjectId(org.bson.types.ObjectId) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Decimal128(org.bson.types.Decimal128) DecimalType(org.apache.flink.table.types.logical.DecimalType) Binary(org.bson.types.Binary) Timestamp(java.sql.Timestamp) Date(java.sql.Date) LocalDate(java.time.LocalDate)

Example 95 with Decimal128

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);
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) SerdeException(io.micronaut.serde.exceptions.SerdeException) BsonWriter(org.bson.BsonWriter) BsonDbPointer(org.bson.BsonDbPointer) Decimal128(org.bson.types.Decimal128) BsonRegularExpression(org.bson.BsonRegularExpression) BsonTimestamp(org.bson.BsonTimestamp) BigDecimal(java.math.BigDecimal) BsonType(org.bson.BsonType) UUID(java.util.UUID)

Aggregations

Decimal128 (org.bson.types.Decimal128)96 ObjectId (org.bson.types.ObjectId)53 Test (org.junit.Test)48 Date (java.util.Date)47 BigDecimal (java.math.BigDecimal)25 AllTypes (io.realm.entities.AllTypes)22 UUID (java.util.UUID)18 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 List (java.util.List)9 BsonDocument (org.bson.BsonDocument)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 Document (org.bson.Document)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 BsonDecimal128 (org.bson.BsonDecimal128)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6