Skip to content

Commit 2f01e7b

Browse files
committed
Add StreamReadCapabililty.EXACT_FLOATS & fix copyCurrentEvent precision loss
1 parent 91a8a0a commit 2f01e7b

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -2468,13 +2468,17 @@ public void copyCurrentEvent(JsonParser p) throws IOException
24682468
}
24692469
case ID_NUMBER_FLOAT:
24702470
{
2471-
NumberType n = p.getNumberType();
2472-
if (n == NumberType.BIG_DECIMAL) {
2473-
writeNumber(p.getDecimalValue());
2474-
} else if (n == NumberType.FLOAT) {
2475-
writeNumber(p.getFloatValue());
2471+
if (p.getReadCapabilities().isEnabled(StreamReadCapability.EXACT_FLOATS)) {
2472+
NumberType n = p.getNumberType();
2473+
if (n == NumberType.BIG_DECIMAL) {
2474+
writeNumber(p.getDecimalValue());
2475+
} else if (n == NumberType.FLOAT) {
2476+
writeNumber(p.getFloatValue());
2477+
} else {
2478+
writeNumber(p.getDoubleValue());
2479+
}
24762480
} else {
2477-
writeNumber(p.getDoubleValue());
2481+
writeNumber(p.getTextCharacters(), p.getTextOffset(), p.getTextLength());
24782482
}
24792483
break;
24802484
}

src/main/java/com/fasterxml/jackson/core/StreamReadCapability.java

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public enum StreamReadCapability
4848
* This capability is true for many textual formats like CSV, Properties and XML.
4949
*/
5050
UNTYPED_SCALARS(false),
51+
52+
/**
53+
* Capability that indicates that data format may report floats
54+
* as a non-exact {@link com.fasterxml.jackson.core.JsonParser.NumberType},
55+
* due to prohibitively expensive parsing costs of determing the precision
56+
* upfront. For example, JSON numbers may be reported as
57+
* {@link com.fasterxml.jackson.core.JsonParser.NumberType#DOUBLE}
58+
* even if they would not fit into a 64-bit double without precision
59+
* loss. Methods like {@link JsonParser#getNumberValueExact()} or
60+
* {@link JsonParser#getValueAsString()} still report values without
61+
* precision loss.
62+
*
63+
* Capability is false for text formats JSON, but true for binary formats
64+
* like Smile, MessagePack, etc., where type is precisely and inexpensively
65+
* signaled by a tag.
66+
*/
67+
EXACT_FLOATS(false)
5168
;
5269

5370
/**

src/test/java/com/fasterxml/jackson/core/json/JsonParserGeneratorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testRoundtripBigDecimal() throws Exception {
2121
generator.copyCurrentEvent(parser);
2222
parser.close();
2323
generator.close();
24-
String actual = stringWriter.toString(); // "Infinity"
24+
String actual = stringWriter.toString();
2525
assertEquals(input, actual);
2626
}
2727
}

0 commit comments

Comments
 (0)