Skip to content

Commit a6b073e

Browse files
committed
JAVA-2296: Support Decimal128 in BasicBSONEncoder for the legacy API.
Note that Decimal128 is NOT supported by BasicBSONDecoder, as that would required the addition of a method to the public intervace BSONCallback.
1 parent 90a39a7 commit a6b073e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

driver/src/main/org/bson/BasicBSONEncoder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bson.types.Binary;
2424
import org.bson.types.Code;
2525
import org.bson.types.CodeWScope;
26+
import org.bson.types.Decimal128;
2627
import org.bson.types.MaxKey;
2728
import org.bson.types.MinKey;
2829
import org.bson.types.ObjectId;
@@ -155,6 +156,8 @@ protected void _putObjectField(final String name, final Object initialValue) {
155156
putDate(name, (Date) value);
156157
} else if (value instanceof Number) {
157158
putNumber(name, (Number) value);
159+
} else if (value instanceof Decimal128) {
160+
putDecimal128(name, (Decimal128) value);
158161
} else if (value instanceof Character) {
159162
putString(name, value.toString());
160163
} else if (value instanceof String) {
@@ -307,6 +310,19 @@ protected void putNumber(final String name, final Number number) {
307310
}
308311
}
309312

313+
/**
314+
* Encodes a Decimal128 field.
315+
*
316+
* @param name the field name
317+
* @param value the value
318+
* @since 3.4
319+
* @mongodb.server.release 3.4
320+
*/
321+
protected void putDecimal128(final String name, final Decimal128 value) {
322+
putName(name);
323+
bsonWriter.writeDecimal128(value);
324+
}
325+
310326
/**
311327
* Encodes a byte array field
312328
*

driver/src/test/unit/org/bson/BasicBSONEncoderSpecification.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.bson.types.BasicBSONList
2424
import org.bson.types.Binary
2525
import org.bson.types.Code
2626
import org.bson.types.CodeWScope
27+
import org.bson.types.Decimal128
2728
import org.bson.types.MaxKey
2829
import org.bson.types.MinKey
2930
import org.bson.types.ObjectId
@@ -88,6 +89,7 @@ class BasicBSONEncoderSpecification extends Specification {
8889
['i': Long.MAX_VALUE] | [16, 0, 0, 0, 18, 105, 0, -1, -1, -1, -1, -1, -1, -1, 127, 0]
8990
['k': new MinKey()] | [8, 0, 0, 0, -1, 107, 0, 0]
9091
['k': new MaxKey()] | [8, 0, 0, 0, 127, 107, 0, 0]
92+
['f': Decimal128.parse('0E-6176')] | [24, 0, 0, 0, 19, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
9193

9294
aClass = document.find { true }.value.getClass()
9395
}

0 commit comments

Comments
 (0)