diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-07-09 17:49:40 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-07-09 17:49:40 -0700 |
commit | 90379ffc88110d3bd4a5452f31ba571087639bb5 (patch) | |
tree | 4223dd0301a94136d946f6c5b6cb58c870e50815 | |
parent | b26d1af90e11d0838f77fd178ded861ec747479c (diff) | |
download | jackson-core-90379ffc88110d3bd4a5452f31ba571087639bb5.tar.gz |
Fixed #631
4 files changed, 38 insertions, 3 deletions
diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index f56c0a23..52714f31 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 4fe901c4..fce2dca3 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonParser.java @@ -1315,10 +1315,41 @@ public abstract class JsonParser * 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. + *<p> + * 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 * {@link JsonToken#VALUE_NUMBER_FLOAT}, returns 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 297c23d4..de04e5a2 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 abstract class ParserBase extends ParserMinimalBase } 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 0bcbd41b..cc62136b 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 class JsonParserDelegate extends JsonParser @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 class JsonParserDelegate extends JsonParser @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 |