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

try to avoid precision loss #983

Merged
merged 6 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -2491,13 +2491,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());
Number n = p.getNumberValueExact();
if (n instanceof Float) {
writeNumber((Float) n);
} else if (n instanceof BigInteger) {
pjfanning marked this conversation as resolved.
Show resolved Hide resolved
writeNumber((BigInteger) n);
} else if (n instanceof BigDecimal) {
final BigDecimal bd = (BigDecimal) n;
p.streamReadConstraints().validateBigIntegerScale(bd.scale());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.... huh?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the 'failing' test case, we end up with getNumberExact returning this exact case (a BigDecimal)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigDecimal makes sense, that validation is what I don't understand (as we are not converting).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I've removed the validation

writeNumber(bd);
} else {
writeNumber(p.getDoubleValue());
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
writeNumber(n.longValue());
}
break;
}
Expand Down Expand Up @@ -2636,13 +2640,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());
Number n = p.getNumberValueExact();
if (n instanceof Float) {
writeNumber((Float) n);
} else if (n instanceof BigInteger) {
writeNumber((BigInteger) n);
} else if (n instanceof BigDecimal) {
final BigDecimal bd = (BigDecimal) n;
p.streamReadConstraints().validateBigIntegerScale(bd.scale());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this validation does not make sense.

writeNumber(bd);
} else {
writeNumber(p.getDoubleValue());
writeNumber(n.longValue());
pjfanning marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ParserPrecisionLoss730Test extends BaseTest
* e.g. for pretty printing a file.
*/
public void testCopyCurrentEventBigDecimal() throws Exception {
String input = "1e999";
String input = "1E+999";
StringWriter stringWriter = new StringWriter();

try (JsonParser parser = JSON_F.createParser(input)) {
Expand All @@ -43,6 +43,6 @@ public void testCopyCurrentStructureBigDecimal() throws Exception {
generator.copyCurrentStructure(parser);
}
}
assertEquals(input, stringWriter.toString());
assertEquals("[1E+999]", stringWriter.toString());
}
}