use of de.undercouch.bson4jackson.types.Decimal128 in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method writeBigDecimalsAsDecimal128s.
/**
* 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());
}
use of de.undercouch.bson4jackson.types.Decimal128 in project bson4jackson by michel-kraemer.
the class BsonGenerator method writeNumber.
@Override
public void writeNumber(BigDecimal dec) throws IOException, JsonGenerationException {
if (isEnabled(Feature.WRITE_BIGDECIMALS_AS_DECIMAL128)) {
Decimal128 d = new Decimal128(dec);
_writeArrayFieldNameIfNeeded();
_verifyValueWrite("write number");
_buffer.putByte(_typeMarker, BsonConstants.TYPE_DECIMAL128);
_buffer.putLong(d.getLow());
_buffer.putLong(d.getHigh());
flushBuffer();
return;
}
if (isEnabled(Feature.WRITE_BIGDECIMALS_AS_STRINGS)) {
writeString(dec.toString());
return;
}
float f = dec.floatValue();
if (!Float.isInfinite(f)) {
writeNumber(f);
} else {
double d = dec.doubleValue();
if (!Double.isInfinite(d)) {
writeNumber(d);
} else {
writeString(dec.toString());
}
}
}
Aggregations