Search in sources :

Example 6 with Decimal128

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

the class JsonReader method visitNumberDecimalExtendedJson.

private Decimal128 visitNumberDecimalExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    Decimal128 value;
    String decimal128AsString = readStringFromExtendedJson();
    try {
        value = Decimal128.parse(decimal128AsString);
    } catch (NumberFormatException e) {
        throw new JsonParseException(format("Exception converting value '%s' to type %s", decimal128AsString, Decimal128.class.getName()), e);
    }
    verifyToken(JsonTokenType.END_OBJECT);
    return value;
}
Also used : Decimal128(org.bson.types.Decimal128)

Example 7 with Decimal128

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

the class Decimal128LegacyAPIQuickTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    DB database = mongoClient.getDB("mydb");
    // get a handle to the "test" collection
    DBCollection collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insert(doc);
    DBObject first = collection.findOne(QueryBuilder.start("amount1").is(new Decimal128(new BigDecimal(".10"))).get());
    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) Decimal128(org.bson.types.Decimal128) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) BigDecimal(java.math.BigDecimal)

Example 8 with Decimal128

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

the class NumberCodecHelper method decodeInt.

static int decodeInt(final BsonReader reader) {
    int intValue;
    BsonType bsonType = reader.getCurrentBsonType();
    switch(bsonType) {
        case INT32:
            intValue = reader.readInt32();
            break;
        case INT64:
            long longValue = reader.readInt64();
            intValue = (int) longValue;
            if (longValue != (long) intValue) {
                throw invalidConversion(Integer.class, longValue);
            }
            break;
        case DOUBLE:
            double doubleValue = reader.readDouble();
            intValue = (int) doubleValue;
            if (doubleValue != (double) intValue) {
                throw invalidConversion(Integer.class, doubleValue);
            }
            break;
        case DECIMAL128:
            Decimal128 decimal128 = reader.readDecimal128();
            intValue = decimal128.intValue();
            if (!decimal128.equals(new Decimal128(intValue))) {
                throw invalidConversion(Integer.class, decimal128);
            }
            break;
        default:
            throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
    }
    return intValue;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128)

Example 9 with Decimal128

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

the class NumberCodecHelper method decodeDouble.

static double decodeDouble(final BsonReader reader) {
    double doubleValue;
    BsonType bsonType = reader.getCurrentBsonType();
    switch(bsonType) {
        case INT32:
            doubleValue = reader.readInt32();
            break;
        case INT64:
            long longValue = reader.readInt64();
            doubleValue = longValue;
            if (longValue != (long) doubleValue) {
                throw invalidConversion(Double.class, longValue);
            }
            break;
        case DOUBLE:
            doubleValue = reader.readDouble();
            break;
        case DECIMAL128:
            Decimal128 decimal128 = reader.readDecimal128();
            try {
                doubleValue = decimal128.doubleValue();
                if (!decimal128.equals(new Decimal128(new BigDecimal(doubleValue)))) {
                    throw invalidConversion(Double.class, decimal128);
                }
            } catch (NumberFormatException e) {
                throw invalidConversion(Double.class, decimal128);
            }
            break;
        default:
            throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
    }
    return doubleValue;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128) BigDecimal(java.math.BigDecimal)

Example 10 with Decimal128

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

the class JsonReaderTest method testDecimal128Int32Constructor.

@Test
public void testDecimal128Int32Constructor() {
    String json = "NumberDecimal(" + Integer.MAX_VALUE + ")";
    testStringAndStream(json, bsonReader -> {
        assertEquals(BsonType.DECIMAL128, bsonReader.readBsonType());
        assertEquals(new Decimal128(Integer.MAX_VALUE), bsonReader.readDecimal128());
        assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
        return null;
    });
}
Also used : Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

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