From dbf0f7c5f8a5009d9e3d79ce1a201ea687f2174a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 6 Apr 2023 23:55:00 +0200 Subject: [PATCH] try to avoid precision loss (#983) --- .../fasterxml/jackson/core/JsonGenerator.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 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..fa593bc2bd 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonGenerator.java @@ -2491,13 +2491,13 @@ 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 BigDecimal) { + writeNumber((BigDecimal) n); } else { - writeNumber(p.getDoubleValue()); + writeNumber(n.doubleValue()); } break; } @@ -2636,13 +2636,13 @@ 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 BigDecimal) { + writeNumber((BigDecimal) n); } else { - writeNumber(p.getDoubleValue()); + writeNumber(n.doubleValue()); } break; }