diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index f56c0a2304..52714f31a1 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -23,6 +23,7 @@ JSON library. #627: Add `JsonParser.isExpectedNumberIntToken()` convenience method #630: Add `StreamWriteCapability` for further format-based/format-agnostic handling improvements +#631: Add `JsonParser.getNumberValueExact()` to allow precision-retaining buffering - Deprecate `JsonParser.getCurrentTokenId()` (use `#currentTokenId()` instead) 2.11.1 (25-Jun-2020) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonParser.java b/src/main/java/com/fasterxml/jackson/core/JsonParser.java index 4fe901c42b..fce2dca319 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonParser.java @@ -1315,9 +1315,40 @@ public int getText(Writer writer) throws IOException, UnsupportedOperationExcept * all kinds of numeric values. It will return the optimal * (simplest/smallest possible) wrapper object that can * express the numeric value just parsed. + * + * @return Numeric value of the current token in its most optimal + * representation + * + * @throws IOException Problem with access: {@link JsonParseException} if + * the current token is not numeric, or if decoding of the value fails + * (invalid format for numbers); plain {@link IOException} if underlying + * content read fails (possible if values are extracted lazily) */ public abstract Number getNumberValue() throws IOException; + /** + * Method similar to {@link #getNumberValue} with the difference that + * for floating-point numbers value returned may be {@link BigDecimal} + * if the underlying format does not store floating-point numbers using + * native representation: for example, textual formats represent numbers + * as Strings (which are 10-based), and conversion to {@link java.lang.Double} + * is potentially lossy operation. + *
+ * Default implementation simply returns {@link #getNumberValue()} + * + * @return Numeric value of the current token using most accurate representation + * + * @throws IOException Problem with access: {@link JsonParseException} if + * the current token is not numeric, or if decoding of the value fails + * (invalid format for numbers); plain {@link IOException} if underlying + * content read fails (possible if values are extracted lazily) + * + * @since 2.12 + */ + public Number getNumberValueExact() throws IOException { + return getNumberValue(); + } + /** * If current token is of type * {@link JsonToken#VALUE_NUMBER_INT} or diff --git a/src/main/java/com/fasterxml/jackson/core/base/ParserBase.java b/src/main/java/com/fasterxml/jackson/core/base/ParserBase.java index 297c23d466..de04e5a28b 100644 --- a/src/main/java/com/fasterxml/jackson/core/base/ParserBase.java +++ b/src/main/java/com/fasterxml/jackson/core/base/ParserBase.java @@ -614,7 +614,7 @@ public Number getNumberValue() throws IOException } return _numberDouble; } - + @Override public NumberType getNumberType() throws IOException { diff --git a/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java b/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java index 0bcbd41b3d..cc62136b19 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java +++ b/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java @@ -193,12 +193,15 @@ public JsonParser overrideFormatFeatures(int values, int mask) { @Override public Number getNumberValue() throws IOException { return delegate.getNumberValue(); } + @Override + public Number getNumberValueExact() throws IOException { return delegate.getNumberValueExact(); } + /* /********************************************************** /* Public API, access to token information, coercion/conversion /********************************************************** */ - + @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); } @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); } @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); } @@ -209,7 +212,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) { @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); } @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); } @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); } - + /* /********************************************************** /* Public API, access to token values, other