Skip to content

Commit

Permalink
Fix #139
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 28, 2018
1 parent 0efa55d commit 9b66e84
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,11 @@ public void writeNumber(BigDecimal dec) throws IOException {
_writeByte(BYTE_TAG_DECIMAL_FRACTION);
_writeByte(BYTE_ARRAY_2_ELEMENTS);

// 27-Nov-2019, tatu: As per [dataformats-binary#139] need to change sign here
int scale = dec.scale();
_writeIntValue(scale);
/* Hmmmh. Specification suggest use of regular integer for mantissa. But
* if it doesn't fit, use "bignum"
*/
_writeIntValue(-scale);
// Hmmmh. Specification suggest use of regular integer for mantissa. But
// if it doesn't fit, use "bignum"
BigInteger unscaled = dec.unscaledValue();
int bitLength = unscaled.bitLength();
if (bitLength <= 31) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ protected JsonToken _handleTaggedArray(int tag, int len) throws IOException
if (t != JsonToken.VALUE_NUMBER_INT) {
_reportError("Unexpected token ("+t+") as the first part of 'bigfloat' value: should get VALUE_NUMBER_INT");
}
int exp = getIntValue();
// 27-Nov-2019, tatu: As per [dataformats-binary#139] need to change sign here
int exp = -getIntValue();

t = nextToken();
// Should get an integer value; int/long/BigInteger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,27 @@ public void testFloatValues() throws Exception
(byte) rawL);
}

// [dataformats-binary#139]: wrong encoding of BigDecimal
public void testBigDecimalValues() throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
CBORGenerator gen = cborGenerator(out);
final BigDecimal NR = new BigDecimal("273.15");
gen.writeNumber(NR);
gen.close();
byte[] b = out.toByteArray();

// [https://tools.ietf.org/html/rfc7049#section-2.4.2]
final byte[] spec = new byte[] {
(byte) 0xC4, // tag 4
(byte) 0x82, // Array of length 2
0x21, // int -- -2
0x19, 0x6a, (byte) 0xb3 // int 27315
};
assertEquals(spec.length, b.length);
Assert.assertArrayEquals(spec, b);
}

public void testEmptyArray() throws Exception
{
// First: empty array (2 bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void testFloatNumberType() throws IOException {
}

public void testBigDecimalType() throws IOException {
final BigDecimal NR = new BigDecimal("273.15");
final BigDecimal NR = new BigDecimal("172.125");
ByteArrayOutputStream out = new ByteArrayOutputStream();
CBORGenerator generator = cborGenerator(out);
generator.writeNumber(NR);
Expand All @@ -343,7 +343,20 @@ public void testBigDecimalType() throws IOException {
assertEquals(NR.intValue(), parser.getIntValue());
assertNull(parser.nextToken());
}
// Almost good. But [dataformats#139] to consider too...
// ... but that'll need to wait for 2.10

// Almost good. But [dataformats#139] to consider too, see
// [https://tools.ietf.org/html/rfc7049#section-2.4.2]
final byte[] spec = new byte[] {
(byte) 0xC4, // tag 4
(byte) 0x82, // Array of length 2
0x21, // int -- -2
0x19, 0x6a, (byte) 0xb3 // int 27315
};
try (CBORParser parser = cborParser(spec)) {
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
assertEquals(NumberType.BIG_DECIMAL, parser.getNumberType());
assertEquals(new BigDecimal("273.15"), parser.getDecimalValue());
assertNull(parser.nextToken());
}
}
}
5 changes: 5 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Project: jackson-datatypes-binaryModules:
=== Releases ===
------------------------------------------------------------------------

2.10.0 (not yet released)

#139: Incorrect decimal fraction representation
(reported by wlukowicz@github)

2.9.7 (19-Sep-2018)

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

0 comments on commit 9b66e84

Please sign in to comment.