Skip to content

Commit 90379ff

Browse files
committed
Fixed #631
1 parent b26d1af commit 90379ff

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ JSON library.
2323
#627: Add `JsonParser.isExpectedNumberIntToken()` convenience method
2424
#630: Add `StreamWriteCapability` for further format-based/format-agnostic
2525
handling improvements
26+
#631: Add `JsonParser.getNumberValueExact()` to allow precision-retaining buffering
2627
- Deprecate `JsonParser.getCurrentTokenId()` (use `#currentTokenId()` instead)
2728

2829
2.11.1 (25-Jun-2020)

src/main/java/com/fasterxml/jackson/core/JsonParser.java

+31
Original file line numberDiff line numberDiff line change
@@ -1315,9 +1315,40 @@ public int getText(Writer writer) throws IOException, UnsupportedOperationExcept
13151315
* all kinds of numeric values. It will return the optimal
13161316
* (simplest/smallest possible) wrapper object that can
13171317
* express the numeric value just parsed.
1318+
*
1319+
* @return Numeric value of the current token in its most optimal
1320+
* representation
1321+
*
1322+
* @throws IOException Problem with access: {@link JsonParseException} if
1323+
* the current token is not numeric, or if decoding of the value fails
1324+
* (invalid format for numbers); plain {@link IOException} if underlying
1325+
* content read fails (possible if values are extracted lazily)
13181326
*/
13191327
public abstract Number getNumberValue() throws IOException;
13201328

1329+
/**
1330+
* Method similar to {@link #getNumberValue} with the difference that
1331+
* for floating-point numbers value returned may be {@link BigDecimal}
1332+
* if the underlying format does not store floating-point numbers using
1333+
* native representation: for example, textual formats represent numbers
1334+
* as Strings (which are 10-based), and conversion to {@link java.lang.Double}
1335+
* is potentially lossy operation.
1336+
*<p>
1337+
* Default implementation simply returns {@link #getNumberValue()}
1338+
*
1339+
* @return Numeric value of the current token using most accurate representation
1340+
*
1341+
* @throws IOException Problem with access: {@link JsonParseException} if
1342+
* the current token is not numeric, or if decoding of the value fails
1343+
* (invalid format for numbers); plain {@link IOException} if underlying
1344+
* content read fails (possible if values are extracted lazily)
1345+
*
1346+
* @since 2.12
1347+
*/
1348+
public Number getNumberValueExact() throws IOException {
1349+
return getNumberValue();
1350+
}
1351+
13211352
/**
13221353
* If current token is of type
13231354
* {@link JsonToken#VALUE_NUMBER_INT} or

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public Number getNumberValue() throws IOException
614614
}
615615
return _numberDouble;
616616
}
617-
617+
618618
@Override
619619
public NumberType getNumberType() throws IOException
620620
{

src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
193193
@Override
194194
public Number getNumberValue() throws IOException { return delegate.getNumberValue(); }
195195

196+
@Override
197+
public Number getNumberValueExact() throws IOException { return delegate.getNumberValueExact(); }
198+
196199
/*
197200
/**********************************************************
198201
/* Public API, access to token information, coercion/conversion
199202
/**********************************************************
200203
*/
201-
204+
202205
@Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); }
203206
@Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); }
204207
@Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); }
@@ -209,7 +212,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
209212
@Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); }
210213
@Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); }
211214
@Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); }
212-
215+
213216
/*
214217
/**********************************************************
215218
/* Public API, access to token values, other

0 commit comments

Comments
 (0)