@Override protected void doWriteDecimal128(final Decimal128 value) { write(new BsonDecimal128(value)); }
@Override public BsonDecimal128 decode(final BsonReader reader, final DecoderContext decoderContext) { return new BsonDecimal128(reader.readDecimal128()); }
@Test public void read() throws Exception { BsonDocument doc = new BsonDocument(); doc.put("int", new BsonDecimal128(Decimal128.parse(Integer.toString(Integer.MAX_VALUE)))); doc.put("long", new BsonDecimal128(new Decimal128(Long.MAX_VALUE))); doc.put("double", new BsonDecimal128(Decimal128.parse("12.111"))); JsonReader reader = Jsons.asGsonReader(doc); reader.beginObject(); check(reader.nextName()).is("int"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextInt()).is(Integer.MAX_VALUE); check(reader.nextName()).is("long"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextLong()).is(Long.MAX_VALUE); check(reader.nextName()).is("double"); check(reader.peek()).is(JsonToken.NUMBER); check(reader.nextDouble()).is(12.111D); reader.endObject(); reader.close(); }
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
private BsonValue constructValue(Object value) { if (value == null) { return BsonNull.VALUE; } else if (value instanceof Double) { return new BsonDouble((Double) value); } else if (value instanceof String) { return new BsonString((String) value); } else if (value instanceof ObjectId) { return new BsonObjectId((ObjectId) value); } else if (value instanceof Integer) { return new BsonInt32((Integer) value); } else if (value instanceof Long) { return new BsonInt64((Long) value); } else if (value instanceof Decimal128) { return new BsonDecimal128((Decimal128) value); } throw new IllegalArgumentException("Unsupported ID type: " + value.getClass()); }
).toInstant()).getTime() )) .append("myDecimal", new BsonDecimal128(new Decimal128(new BigDecimal("12345.6789"))));
com.torodb.mongowp.bson.BsonDecimal128 casted = (com.torodb.mongowp.bson.BsonDecimal128) value; return new org.bson.BsonDecimal128( Decimal128.fromIEEE754BIDEncoding(casted.getHigh(), casted.getLow()));