@Override public double doubleValue() { return value.bigDecimalValue().doubleValue(); }
@Override public long longValue() { return value.bigDecimalValue().longValue(); }
@Override public int intValue() { return value.bigDecimalValue().intValue(); }
@Override public BigDecimal decode(final BsonReader reader, final DecoderContext decoderContext) { return reader.readDecimal128().bigDecimalValue(); }
@Override public BigDecimal read(JsonReader in) throws IOException { checkArgument(in instanceof BsonReader, "Should be BsonReader, not some other JsonReader"); return ((BsonReader) in).unwrap().readDecimal128().bigDecimalValue(); }
@Override public Object decode(final Class<?> targetClass, final Object value, final MappedField optionalExtraInfo) { if (value == null) { return null; } if (value instanceof BigDecimal) { return value; } if (value instanceof Decimal128) { return ((Decimal128) value).bigDecimalValue(); } if (value instanceof BigInteger) { return new BigDecimal(((BigInteger) value)); } if (value instanceof Double) { return new BigDecimal(((Double) value)); } if (value instanceof Long) { return new BigDecimal(((Long) value)); } if (value instanceof Number) { return new BigDecimal(((Number) value).doubleValue()); } return new BigDecimal(value.toString()); }
@Override public Number getNumberValue() throws IOException { final BsonType type = type(); switch (type) { case DOUBLE: return reader.readDouble(); case INT32: return reader.readInt32(); case INT64: return reader.readInt64(); case DECIMAL128: return reader.readDecimal128().bigDecimalValue(); case STRING: return new BigDecimal(reader.readString()); default: throw new IllegalStateException(String.format("Can't convert %s to %s", type, Number.class.getName())); } }
@Override public double nextDouble() throws IOException { switch (delegate.getCurrentBsonType()) { case INT32: return delegate.readInt32(); case INT64: return delegate.readInt64(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().doubleValue(); default: return delegate.readDouble(); } }
@Override public long nextLong() throws IOException { switch (delegate.getCurrentBsonType()) { case DOUBLE: return (long) delegate.readDouble(); case INT32: return delegate.readInt32(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().longValueExact(); default: return delegate.readInt64(); } }
@Override public int nextInt() throws IOException { switch (delegate.getCurrentBsonType()) { case DOUBLE: return (int) delegate.readDouble(); case INT64: return (int) delegate.readInt64(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().intValueExact(); default: return delegate.readInt32(); } }
@Test public void write() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument doc = Jsons.toBson(obj); check(doc.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(doc.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(doc.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); }
@Test public void bigNumbers() throws Exception { JsonObject obj = new JsonObject(); BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128")); obj.addProperty("bigInteger", bigInteger); BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024)); obj.addProperty("bigDecimal", bigDecimal); BsonDocument bson = Jsons.toBson(obj); check(bson.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger); check(bson.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128); check(bson.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal); check(Jsons.toGson(bson)).is(obj); } }
@Override public BigDecimal decode(BsonReader reader, DecoderContext decoderContext) { return reader.readDecimal128().bigDecimalValue(); }
@Override public Object decode(final Class<?> targetClass, final Object value, final MappedField optionalExtraInfo) { if (value == null) { return null; } if (value instanceof BigDecimal) { return value; } if (value instanceof Decimal128) { return ((Decimal128) value).bigDecimalValue(); } if (value instanceof BigInteger) { return new BigDecimal(((BigInteger) value)); } if (value instanceof Double) { return new BigDecimal(((Double) value)); } if (value instanceof Long) { return new BigDecimal(((Long) value)); } if (value instanceof Number) { return new BigDecimal(((Number) value).doubleValue()); } return new BigDecimal(value.toString()); }
@Override public BigDecimal read(JsonReader in) throws IOException { checkArgument(in instanceof BsonReader, "Should be BsonReader, not some other JsonReader"); return ((BsonReader) in).unwrap().readDecimal128().bigDecimalValue(); }
@Override public Number getNumberValue() throws IOException { final BsonType type = type(); switch (type) { case DOUBLE: return reader.readDouble(); case INT32: return reader.readInt32(); case INT64: return reader.readInt64(); case DECIMAL128: return reader.readDecimal128().bigDecimalValue(); case STRING: return new BigDecimal(reader.readString()); default: throw new IllegalStateException(String.format("Can't convert %s to %s", type, Number.class.getName())); } }
@Override public double nextDouble() throws IOException { switch (delegate.getCurrentBsonType()) { case INT32: return delegate.readInt32(); case INT64: return delegate.readInt64(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().doubleValue(); default: return delegate.readDouble(); } }
@Override public long nextLong() throws IOException { switch (delegate.getCurrentBsonType()) { case DOUBLE: return (long) delegate.readDouble(); case INT32: return delegate.readInt32(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().longValueExact(); default: return delegate.readInt64(); } }
@Override public int nextInt() throws IOException { switch (delegate.getCurrentBsonType()) { case DOUBLE: return (int) delegate.readDouble(); case INT64: return (int) delegate.readInt64(); case DECIMAL128: return delegate.readDecimal128().bigDecimalValue().intValueExact(); default: return delegate.readInt32(); } }
/** * Test if {@link BigDecimal} objects can be serialized as {@link Decimal128}s * @throws Exception if something goes wrong */ @Test public void writeBigDecimalsAsDecimal128s() throws Exception { Map<String, Object> data = new LinkedHashMap<String, Object>(); data.put("big", new BigDecimal("0.3")); BSONObject obj = generateAndParse(data); Double result = (Double)obj.get("big"); //BigDecimal("0.3") does not equal 0.3! assertEquals(0.3, result, 0.000001); assertFalse(Double.valueOf(0.3).equals(result)); data = new LinkedHashMap<String, Object>(); data.put("big", new BigDecimal("0.3")); obj = generateAndParse(data, Feature.WRITE_BIGDECIMALS_AS_DECIMAL128); org.bson.types.Decimal128 strResult = (org.bson.types.Decimal128)obj.get("big"); assertEquals(new BigDecimal("0.3"), strResult.bigDecimalValue()); }