Skip to content

Commit

Permalink
Fixes #1149: add JsonParser.getNumberTypeFP()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 5, 2024
1 parent d14acac commit 2d1fa8b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 59 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 2d1fa8b

Please sign in to comment.