diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 67561656c5..1e3f64a2fb 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,6 +18,7 @@ a pure JSON library. #1145: `JsonPointer.appendProperty(String)` does not escape the property name (reported by Robert E) +#1149: Add `JsonParser.getNumberTypeFP()` #1157: Use fast parser (FDP) for large `BigDecimal`s (500+ chars) (contributed by @pjfanning) #1169: `ArrayIndexOutOfBoundsException` for specific invalid content, diff --git a/src/main/java/com/fasterxml/jackson/core/JsonParser.java b/src/main/java/com/fasterxml/jackson/core/JsonParser.java index 3dddc1cbf2..a0e619f82e 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonParser.java @@ -40,7 +40,47 @@ public abstract class JsonParser */ public enum NumberType { INT, LONG, BIG_INTEGER, FLOAT, DOUBLE, BIG_DECIMAL - }; + } + + /** + * Enumeration of possible physical Floating-Point types that + * underlying format uses. Used to indicate most accurate (and + * efficient) representation if known (if not known, + * {@link NumberTypeFP#UNKNOWN} is used). + * + * @since 2.17 + */ + public enum NumberTypeFP { + /** + * Special "mini-float" that some binary formats support. + */ + FLOAT16, + + /** + * Standard IEEE-754 single-precision 32-bit binary value + */ + FLOAT32, + + /** + * Standard IEEE-754 double-precision 64-bit binary value + */ + DOUBLE64, + + /** + * Unlimited precision, decimal (10-based) values + */ + BIG_DECIMAL, + + /** + * Constant used when type is not known, or there is no specific + * type to match: most commonly used for textual formats like JSON + * where representation does not necessarily have single easily detectable + * optimal representation (for example, value {@code 0.1} has no + * exact binary representation whereas {@code 0.25} has exact representation + * in every binary type supported) + */ + UNKNOWN; + } /** * Default set of {@link StreamReadCapability}ies that may be used as @@ -1715,7 +1755,7 @@ public Object getNumberValueDeferred() throws IOException { * If current token is of type * {@link JsonToken#VALUE_NUMBER_INT} or * {@link JsonToken#VALUE_NUMBER_FLOAT}, returns - * one of {@link NumberType} constants; otherwise returns null. + * one of {@link NumberType} constants; otherwise returns {@code null}. * * @return Type of current number, if parser points to numeric token; {@code null} otherwise * @@ -1724,6 +1764,23 @@ public Object getNumberValueDeferred() throws IOException { */ public abstract NumberType getNumberType() throws IOException; + /** + * If current token is of type + * {@link JsonToken#VALUE_NUMBER_FLOAT}, returns + * one of {@link NumberTypeFP} constants; otherwise returns + * {@link NumberTypeFP#UNKNOWN}. + * + * @return Type of current number, if parser points to numeric token; {@code null} otherwise + * + * @throws IOException for low-level read issues, or + * {@link JsonParseException} for decoding problems + * + * @since 2.17 + */ + public NumberTypeFP getNumberTypeFP() throws IOException { + return NumberTypeFP.UNKNOWN; + } + /** * Numeric accessor that can be called when the current * token is of type {@link JsonToken#VALUE_NUMBER_INT} and 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 fdeb557a3b..f69e85002a 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java +++ b/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java @@ -244,6 +244,9 @@ public boolean requiresCustomCodec() { @Override public NumberType getNumberType() throws IOException { return delegate.getNumberType(); } + @Override + public NumberTypeFP getNumberTypeFP() throws IOException { return delegate.getNumberTypeFP(); } + @Override public Number getNumberValue() throws IOException { return delegate.getNumberValue(); } diff --git a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java index cc76217620..1cf41b19e6 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java @@ -71,6 +71,7 @@ private void _testSimpleInt(int EXP_I, int mode) throws Exception assertToken(JsonToken.START_ARRAY, p.nextToken()); assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(JsonParser.NumberType.INT, p.getNumberType()); + assertEquals(JsonParser.NumberTypeFP.UNKNOWN, p.getNumberTypeFP()); assertTrue(p.isExpectedNumberIntToken()); assertEquals(""+EXP_I, p.getText()); diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java b/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java index 1cdc1e9ec6..03acb78aac 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java +++ b/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.JsonParser.NumberType; +import com.fasterxml.jackson.core.JsonParser.NumberTypeFP; import com.fasterxml.jackson.core.type.ResolvedType; import com.fasterxml.jackson.core.type.TypeReference; @@ -263,6 +264,7 @@ public void testParserDelegate() throws IOException assertFalse(del.isNaN()); assertTrue(del.isExpectedNumberIntToken()); assertEquals(NumberType.INT, del.getNumberType()); + assertEquals(NumberTypeFP.UNKNOWN, del.getNumberTypeFP()); assertEquals(Integer.valueOf(1), del.getNumberValue()); assertNull(del.getEmbeddedObject());