From f2e845a5ad4279e8f5f0e45f45609da2dc8d2913 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:08:56 +0200 Subject: [PATCH 1/6] try to avoid precision loss --- .../fasterxml/jackson/core/JsonGenerator.java | 20 ++++++------------- .../failing/ParserPrecisionLoss730Test.java | 4 ++-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index fbc56a8d3b..f5d68b3127 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2482,22 +2482,18 @@ public void copyCurrentEvent(JsonParser p) throws IOException NumberType n = p.getNumberType(); if (n == NumberType.INT) { writeNumber(p.getIntValue()); - } else if (n == NumberType.BIG_INTEGER) { - writeNumber(p.getBigIntegerValue()); } else { - writeNumber(p.getLongValue()); + writeNumber(p.getBigIntegerValue()); } break; } case ID_NUMBER_FLOAT: { NumberType n = p.getNumberType(); - if (n == NumberType.BIG_DECIMAL) { - writeNumber(p.getDecimalValue()); - } else if (n == NumberType.FLOAT) { + if (n == NumberType.FLOAT) { writeNumber(p.getFloatValue()); } else { - writeNumber(p.getDoubleValue()); + writeNumber(p.getDecimalValue()); } break; } @@ -2627,22 +2623,18 @@ protected void _copyCurrentContents(JsonParser p) throws IOException NumberType n = p.getNumberType(); if (n == NumberType.INT) { writeNumber(p.getIntValue()); - } else if (n == NumberType.BIG_INTEGER) { - writeNumber(p.getBigIntegerValue()); } else { - writeNumber(p.getLongValue()); + writeNumber(p.getBigIntegerValue()); } break; } case ID_NUMBER_FLOAT: { NumberType n = p.getNumberType(); - if (n == NumberType.BIG_DECIMAL) { - writeNumber(p.getDecimalValue()); - } else if (n == NumberType.FLOAT) { + if (n == NumberType.FLOAT) { writeNumber(p.getFloatValue()); } else { - writeNumber(p.getDoubleValue()); + writeNumber(p.getDecimalValue()); } break; } diff --git a/src/test/java/com/fasterxml/jackson/failing/ParserPrecisionLoss730Test.java b/src/test/java/com/fasterxml/jackson/failing/ParserPrecisionLoss730Test.java index aad7abc4d3..9b68865b5c 100644 --- a/src/test/java/com/fasterxml/jackson/failing/ParserPrecisionLoss730Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/ParserPrecisionLoss730Test.java @@ -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)) { @@ -43,6 +43,6 @@ public void testCopyCurrentStructureBigDecimal() throws Exception { generator.copyCurrentStructure(parser); } } - assertEquals(input, stringWriter.toString()); + assertEquals("[1E+999]", stringWriter.toString()); } } From a1add20cd1eb0131864f90310e06e610c0c858f1 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:32:32 +0200 Subject: [PATCH 2/6] revert int/long changes --- .../java/com/fasterxml/jackson/core/JsonGenerator.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index f5d68b3127..ce06263d57 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2482,8 +2482,10 @@ public void copyCurrentEvent(JsonParser p) throws IOException NumberType n = p.getNumberType(); if (n == NumberType.INT) { writeNumber(p.getIntValue()); - } else { + } else if (n == NumberType.BIG_INTEGER) { writeNumber(p.getBigIntegerValue()); + } else { + writeNumber(p.getLongValue()); } break; } @@ -2623,8 +2625,10 @@ protected void _copyCurrentContents(JsonParser p) throws IOException NumberType n = p.getNumberType(); if (n == NumberType.INT) { writeNumber(p.getIntValue()); - } else { + } else if (n == NumberType.BIG_INTEGER) { writeNumber(p.getBigIntegerValue()); + } else { + writeNumber(p.getLongValue()); } break; } From 205d53af5b5c71beb04d6dae5023edcf90f24464 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:39:00 +0200 Subject: [PATCH 3/6] Update JsonGenerator.java --- .../fasterxml/jackson/core/JsonGenerator.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index ce06263d57..0d0b176735 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2491,11 +2491,17 @@ public void copyCurrentEvent(JsonParser p) throws IOException } case ID_NUMBER_FLOAT: { - NumberType n = p.getNumberType(); - 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()); + writeNumber(bd); } else { - writeNumber(p.getDecimalValue()); + writeNumber(n.longValue()); } break; } @@ -2634,11 +2640,17 @@ protected void _copyCurrentContents(JsonParser p) throws IOException } case ID_NUMBER_FLOAT: { - NumberType n = p.getNumberType(); - 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()); + writeNumber(bd); } else { - writeNumber(p.getDecimalValue()); + writeNumber(n.longValue()); } break; } From 773c40f1dc64813ffdebcba506b968b139089c78 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:44:41 +0200 Subject: [PATCH 4/6] Update JsonGenerator.java --- src/main/java/com/fasterxml/jackson/core/JsonGenerator.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index 0d0b176735..d5c3473f9c 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2494,8 +2494,6 @@ public void copyCurrentEvent(JsonParser p) throws IOException 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()); @@ -2643,8 +2641,6 @@ protected void _copyCurrentContents(JsonParser p) throws IOException 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()); From e437833500115a92a30bf14847fbe62523c94883 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:47:34 +0200 Subject: [PATCH 5/6] Update JsonGenerator.java --- .../java/com/fasterxml/jackson/core/JsonGenerator.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index d5c3473f9c..588005c729 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2495,9 +2495,7 @@ public void copyCurrentEvent(JsonParser p) throws IOException if (n instanceof Float) { writeNumber((Float) n); } else if (n instanceof BigDecimal) { - final BigDecimal bd = (BigDecimal) n; - p.streamReadConstraints().validateBigIntegerScale(bd.scale()); - writeNumber(bd); + writeNumber((BigDecimal) n); } else { writeNumber(n.longValue()); } @@ -2642,9 +2640,7 @@ protected void _copyCurrentContents(JsonParser p) throws IOException if (n instanceof Float) { writeNumber((Float) n); } else if (n instanceof BigDecimal) { - final BigDecimal bd = (BigDecimal) n; - p.streamReadConstraints().validateBigIntegerScale(bd.scale()); - writeNumber(bd); + writeNumber((BigDecimal) n); } else { writeNumber(n.longValue()); } From b9c07cc39fd61c182dbab570fb649055d9efe304 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:48:50 +0200 Subject: [PATCH 6/6] Update JsonGenerator.java --- src/main/java/com/fasterxml/jackson/core/JsonGenerator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java index 588005c729..fa593bc2bd 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2497,7 +2497,7 @@ public void copyCurrentEvent(JsonParser p) throws IOException } else if (n instanceof BigDecimal) { writeNumber((BigDecimal) n); } else { - writeNumber(n.longValue()); + writeNumber(n.doubleValue()); } break; } @@ -2642,7 +2642,7 @@ protected void _copyCurrentContents(JsonParser p) throws IOException } else if (n instanceof BigDecimal) { writeNumber((BigDecimal) n); } else { - writeNumber(n.longValue()); + writeNumber(n.doubleValue()); } break; }