Skip to content

Commit ff86e1f

Browse files
committed
Add support for BigDecimal Documents in CBOR
1 parent 8d84a26 commit ff86e1f

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

codecs/cbor-codec/src/main/java/software/amazon/smithy/java/cbor/CborDeserializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ public Document readDocument() {
316316
}
317317
}
318318
case Token.POS_BIGINT, Token.NEG_BIGINT -> Document.of(readBigInteger(null));
319+
case Token.BIG_DECIMAL -> Document.of(readBigDecimal(null));
319320
case Token.START_ARRAY -> {
320321
List<Document> values = new ArrayList<>();
321322
for (token = parser.advance(); token != Token.END_ARRAY; token = parser.advance()) {

codecs/cbor-codec/src/test/java/software/amazon/smithy/java/cbor/CborDocumentTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,28 @@ public static List<Arguments> checkEqualitySource() {
391391
Arguments.of("{\"foo\":\"foo\"}", "{\"foo\":\"bar\"}", false));
392392
}
393393

394+
@ParameterizedTest
395+
@MethodSource("bigDecimalDocumentSource")
396+
public void convertsBigDecimalToDocument(BigDecimal bigDecimal) {
397+
var codec = Rpcv2CborCodec.builder().build();
398+
var ser = codec.serialize(Document.of(bigDecimal));
399+
var document = codec.createDeserializer(ser).readDocument();
400+
401+
assertThat(document.type(), is(ShapeType.BIG_DECIMAL));
402+
assertThat(document.asBigDecimal(), comparesEqualTo(bigDecimal));
403+
assertEquals(document, Document.of(bigDecimal));
404+
}
405+
406+
public static List<Arguments> bigDecimalDocumentSource() {
407+
return List.of(
408+
Arguments.of(new BigDecimal("12345.6789")),
409+
Arguments.of(new BigDecimal("0.00001")),
410+
Arguments.of(new BigDecimal("9999999999999999999999999999.123456789")),
411+
Arguments.of(new BigDecimal("3.14159")),
412+
Arguments.of(new BigDecimal("-42.5")),
413+
Arguments.of(BigDecimal.ZERO));
414+
}
415+
394416
@Test
395417
public void canNormalizeCborDocuments() {
396418
var de = toCbor("true");

0 commit comments

Comments
 (0)