Skip to content

Commit 9b66e84

Browse files
committed
Fix #139
1 parent 0efa55d commit 9b66e84

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORGenerator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1032,11 +1032,11 @@ public void writeNumber(BigDecimal dec) throws IOException {
10321032
_writeByte(BYTE_TAG_DECIMAL_FRACTION);
10331033
_writeByte(BYTE_ARRAY_2_ELEMENTS);
10341034

1035+
// 27-Nov-2019, tatu: As per [dataformats-binary#139] need to change sign here
10351036
int scale = dec.scale();
1036-
_writeIntValue(scale);
1037-
/* Hmmmh. Specification suggest use of regular integer for mantissa. But
1038-
* if it doesn't fit, use "bignum"
1039-
*/
1037+
_writeIntValue(-scale);
1038+
// Hmmmh. Specification suggest use of regular integer for mantissa. But
1039+
// if it doesn't fit, use "bignum"
10401040
BigInteger unscaled = dec.unscaledValue();
10411041
int bitLength = unscaled.bitLength();
10421042
if (bitLength <= 31) {

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,8 @@ protected JsonToken _handleTaggedArray(int tag, int len) throws IOException
882882
if (t != JsonToken.VALUE_NUMBER_INT) {
883883
_reportError("Unexpected token ("+t+") as the first part of 'bigfloat' value: should get VALUE_NUMBER_INT");
884884
}
885-
int exp = getIntValue();
885+
// 27-Nov-2019, tatu: As per [dataformats-binary#139] need to change sign here
886+
int exp = -getIntValue();
886887

887888
t = nextToken();
888889
// Should get an integer value; int/long/BigInteger

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/GeneratorSimpleTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ public void testFloatValues() throws Exception
197197
(byte) rawL);
198198
}
199199

200+
// [dataformats-binary#139]: wrong encoding of BigDecimal
201+
public void testBigDecimalValues() throws Exception
202+
{
203+
ByteArrayOutputStream out = new ByteArrayOutputStream();
204+
CBORGenerator gen = cborGenerator(out);
205+
final BigDecimal NR = new BigDecimal("273.15");
206+
gen.writeNumber(NR);
207+
gen.close();
208+
byte[] b = out.toByteArray();
209+
210+
// [https://tools.ietf.org/html/rfc7049#section-2.4.2]
211+
final byte[] spec = new byte[] {
212+
(byte) 0xC4, // tag 4
213+
(byte) 0x82, // Array of length 2
214+
0x21, // int -- -2
215+
0x19, 0x6a, (byte) 0xb3 // int 27315
216+
};
217+
assertEquals(spec.length, b.length);
218+
Assert.assertArrayEquals(spec, b);
219+
}
220+
200221
public void testEmptyArray() throws Exception
201222
{
202223
// First: empty array (2 bytes)

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParserNumbersTest.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public void testFloatNumberType() throws IOException {
328328
}
329329

330330
public void testBigDecimalType() throws IOException {
331-
final BigDecimal NR = new BigDecimal("273.15");
331+
final BigDecimal NR = new BigDecimal("172.125");
332332
ByteArrayOutputStream out = new ByteArrayOutputStream();
333333
CBORGenerator generator = cborGenerator(out);
334334
generator.writeNumber(NR);
@@ -343,7 +343,20 @@ public void testBigDecimalType() throws IOException {
343343
assertEquals(NR.intValue(), parser.getIntValue());
344344
assertNull(parser.nextToken());
345345
}
346-
// Almost good. But [dataformats#139] to consider too...
347-
// ... but that'll need to wait for 2.10
346+
347+
// Almost good. But [dataformats#139] to consider too, see
348+
// [https://tools.ietf.org/html/rfc7049#section-2.4.2]
349+
final byte[] spec = new byte[] {
350+
(byte) 0xC4, // tag 4
351+
(byte) 0x82, // Array of length 2
352+
0x21, // int -- -2
353+
0x19, 0x6a, (byte) 0xb3 // int 27315
354+
};
355+
try (CBORParser parser = cborParser(spec)) {
356+
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
357+
assertEquals(NumberType.BIG_DECIMAL, parser.getNumberType());
358+
assertEquals(new BigDecimal("273.15"), parser.getDecimalValue());
359+
assertNull(parser.nextToken());
360+
}
348361
}
349362
}

release-notes/VERSION

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Project: jackson-datatypes-binaryModules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.10.0 (not yet released)
12+
13+
#139: Incorrect decimal fraction representation
14+
(reported by wlukowicz@github)
15+
1116
2.9.7 (19-Sep-2018)
1217

1318
#142: (ion) `IonParser.getNumberType()` returns `null` for `IonType.FLOAT`

0 commit comments

Comments
 (0)