Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StreamReadCapability.EXACT_FLOATS to indicate whether parser can expose exact floating-point values or not #733

Merged
merged 6 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 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 Expand Up @@ -2613,13 +2617,17 @@ protected void _copyCurrentContents(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),

/**
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
* 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
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.core.json;

import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;

import java.io.ByteArrayOutputStream;
import java.io.StringWriter;

public class JsonParserGeneratorTest
extends BaseTest {
final JsonFactory JSON_F = newStreamFactory();

public void testCopyCurrentEventBigDecimal() throws Exception {
String input = "1e999";
JsonParser parser = JSON_F.createParser(input);
parser.nextToken();
StringWriter stringWriter = new StringWriter();
JsonGenerator generator = JSON_F.createGenerator(stringWriter);
generator.copyCurrentEvent(parser);
parser.close();
generator.close();
String actual = stringWriter.toString();
assertEquals(input, actual);
}

public void testCopyCurrentStructureBigDecimal() throws Exception {
String input = "[1e999]";
JsonParser parser = JSON_F.createParser(input);
parser.nextToken();
StringWriter stringWriter = new StringWriter();
JsonGenerator generator = JSON_F.createGenerator(stringWriter);
generator.copyCurrentStructure(parser);
parser.close();
generator.close();
String actual = stringWriter.toString();
assertEquals(input, actual);
}
}