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