Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1149: add JsonParser.getNumberTypeFP() #1182

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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