Skip to content

Commit 2d1fa8b

Browse files
committed
Fixes #1149: add JsonParser.getNumberTypeFP()
1 parent d14acac commit 2d1fa8b

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

Diff for: release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ a pure JSON library.
1818

1919
#1145: `JsonPointer.appendProperty(String)` does not escape the property name
2020
(reported by Robert E)
21+
#1149: Add `JsonParser.getNumberTypeFP()`
2122
#1157: Use fast parser (FDP) for large `BigDecimal`s (500+ chars)
2223
(contributed by @pjfanning)
2324
#1169: `ArrayIndexOutOfBoundsException` for specific invalid content,

Diff for: src/main/java/com/fasterxml/jackson/core/JsonParser.java

+59-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,47 @@ public abstract class JsonParser
4040
*/
4141
public enum NumberType {
4242
INT, LONG, BIG_INTEGER, FLOAT, DOUBLE, BIG_DECIMAL
43-
};
43+
}
44+
45+
/**
46+
* Enumeration of possible physical Floating-Point types that
47+
* underlying format uses. Used to indicate most accurate (and
48+
* efficient) representation if known (if not known,
49+
* {@link NumberTypeFP#UNKNOWN} is used).
50+
*
51+
* @since 2.17
52+
*/
53+
public enum NumberTypeFP {
54+
/**
55+
* Special "mini-float" that some binary formats support.
56+
*/
57+
FLOAT16,
58+
59+
/**
60+
* Standard IEEE-754 single-precision 32-bit binary value
61+
*/
62+
FLOAT32,
63+
64+
/**
65+
* Standard IEEE-754 double-precision 64-bit binary value
66+
*/
67+
DOUBLE64,
68+
69+
/**
70+
* Unlimited precision, decimal (10-based) values
71+
*/
72+
BIG_DECIMAL,
73+
74+
/**
75+
* Constant used when type is not known, or there is no specific
76+
* type to match: most commonly used for textual formats like JSON
77+
* where representation does not necessarily have single easily detectable
78+
* optimal representation (for example, value {@code 0.1} has no
79+
* exact binary representation whereas {@code 0.25} has exact representation
80+
* in every binary type supported)
81+
*/
82+
UNKNOWN;
83+
}
4484

4585
/**
4686
* Default set of {@link StreamReadCapability}ies that may be used as
@@ -1715,7 +1755,7 @@ public Object getNumberValueDeferred() throws IOException {
17151755
* If current token is of type
17161756
* {@link JsonToken#VALUE_NUMBER_INT} or
17171757
* {@link JsonToken#VALUE_NUMBER_FLOAT}, returns
1718-
* one of {@link NumberType} constants; otherwise returns null.
1758+
* one of {@link NumberType} constants; otherwise returns {@code null}.
17191759
*
17201760
* @return Type of current number, if parser points to numeric token; {@code null} otherwise
17211761
*
@@ -1724,6 +1764,23 @@ public Object getNumberValueDeferred() throws IOException {
17241764
*/
17251765
public abstract NumberType getNumberType() throws IOException;
17261766

1767+
/**
1768+
* If current token is of type
1769+
* {@link JsonToken#VALUE_NUMBER_FLOAT}, returns
1770+
* one of {@link NumberTypeFP} constants; otherwise returns
1771+
* {@link NumberTypeFP#UNKNOWN}.
1772+
*
1773+
* @return Type of current number, if parser points to numeric token; {@code null} otherwise
1774+
*
1775+
* @throws IOException for low-level read issues, or
1776+
* {@link JsonParseException} for decoding problems
1777+
*
1778+
* @since 2.17
1779+
*/
1780+
public NumberTypeFP getNumberTypeFP() throws IOException {
1781+
return NumberTypeFP.UNKNOWN;
1782+
}
1783+
17271784
/**
17281785
* Numeric accessor that can be called when the current
17291786
* token is of type {@link JsonToken#VALUE_NUMBER_INT} and

Diff for: src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java

+3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ public boolean requiresCustomCodec() {
244244
@Override
245245
public NumberType getNumberType() throws IOException { return delegate.getNumberType(); }
246246

247+
@Override
248+
public NumberTypeFP getNumberTypeFP() throws IOException { return delegate.getNumberTypeFP(); }
249+
247250
@Override
248251
public Number getNumberValue() throws IOException { return delegate.getNumberValue(); }
249252

Diff for: src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void _testSimpleInt(int EXP_I, int mode) throws Exception
7171
assertToken(JsonToken.START_ARRAY, p.nextToken());
7272
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
7373
assertEquals(JsonParser.NumberType.INT, p.getNumberType());
74+
assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
7475
assertTrue(p.isExpectedNumberIntToken());
7576
assertEquals(""+EXP_I, p.getText());
7677

Diff for: src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.core.*;
99
import com.fasterxml.jackson.core.JsonParser.NumberType;
10+
import com.fasterxml.jackson.core.JsonParser.NumberTypeFP;
1011
import com.fasterxml.jackson.core.type.ResolvedType;
1112
import com.fasterxml.jackson.core.type.TypeReference;
1213

@@ -263,6 +264,7 @@ public void testParserDelegate() throws IOException
263264
assertFalse(del.isNaN());
264265
assertTrue(del.isExpectedNumberIntToken());
265266
assertEquals(NumberType.INT, del.getNumberType());
267+
assertEquals(NumberTypeFP.UNKNOWN, del.getNumberTypeFP());
266268
assertEquals(Integer.valueOf(1), del.getNumberValue());
267269
assertNull(del.getEmbeddedObject());
268270

0 commit comments

Comments
 (0)