Skip to content

Commit

Permalink
Add StreamReadCapabililty.EXACT_FLOATS & fix copyCurrentEvent precisi…
Browse files Browse the repository at this point in the history
…on loss
  • Loading branch information
htmldoug committed Dec 20, 2021
1 parent 8c74f6f commit 33a4c64
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2468,13 +2468,17 @@ public void copyCurrentEvent(JsonParser p) throws IOException
}
case ID_NUMBER_FLOAT:
{
NumberType n = p.getNumberType();
if (n == NumberType.BIG_DECIMAL) {
writeNumber(p.getDecimalValue());
} else if (n == NumberType.FLOAT) {
writeNumber(p.getFloatValue());
if (p.getReadCapabilities().isEnabled(StreamReadCapability.EXACT_FLOATS)) {
NumberType n = p.getNumberType();
if (n == NumberType.BIG_DECIMAL) {
writeNumber(p.getDecimalValue());
} else if (n == NumberType.FLOAT) {
writeNumber(p.getFloatValue());
} else {
writeNumber(p.getDoubleValue());
}
} else {
writeNumber(p.getDoubleValue());
writeNumber(p.getTextCharacters(), p.getTextOffset(), p.getTextLength());
}
break;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/StreamReadCapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public enum StreamReadCapability
* This capability is true for many textual formats like CSV, Properties and XML.
*/
UNTYPED_SCALARS(false),

/**
* Capability that indicates that data format may report floats
* as a non-exact {@link com.fasterxml.jackson.core.JsonParser.NumberType},
* due to prohibitively expensive parsing costs of determing the precision
* upfront. For example, JSON numbers may be reported as
* {@link com.fasterxml.jackson.core.JsonParser.NumberType#DOUBLE}
* even if they would not fit into a 64-bit double without precision
* loss. Methods like {@link JsonParser#getNumberValueExact()} or
* {@link JsonParser#getValueAsString()} still report values without
* precision loss.
*
* Capability is false for text formats JSON, but true for binary formats
* like Smile, MessagePack, etc., where type is precisely and inexpensively
* signaled by a tag.
*/
EXACT_FLOATS(false)
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testRoundtripBigDecimal() throws Exception {
generator.copyCurrentEvent(parser);
parser.close();
generator.close();
String actual = stringWriter.toString(); // "Infinity"
String actual = stringWriter.toString();
assertEquals(input, actual);
}
}

0 comments on commit 33a4c64

Please sign in to comment.