Skip to content

Commit 182869c

Browse files
committed
Sync with core#1434 change wrt JsonParser.getNumberType()
1 parent cea91f3 commit 182869c

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

cbor/src/main/java/tools/jackson/dataformat/cbor/CBORParser.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,7 @@ public boolean isNaN() {
20112011
public Number getNumberValue() throws JacksonException
20122012
{
20132013
if (_numTypesValid == NR_UNKNOWN) {
2014-
_checkNumericValue(NR_UNKNOWN); // will also check event type
2014+
_checkNumericValue(); // will also check event type
20152015
}
20162016
// Separate types for int types
20172017
if (_currToken == JsonToken.VALUE_NUMBER_INT) {
@@ -2050,9 +2050,6 @@ public final Number getNumberValueExact() throws JacksonException {
20502050
@Override
20512051
public NumberType getNumberType() throws JacksonException
20522052
{
2053-
if (_numTypesValid == NR_UNKNOWN) {
2054-
_checkNumericValue(NR_UNKNOWN); // will also check event type
2055-
}
20562053
if (_currToken == JsonToken.VALUE_NUMBER_INT) {
20572054
if ((_numTypesValid & NR_INT) != 0) {
20582055
return NumberType.INT;
@@ -2062,23 +2059,25 @@ public NumberType getNumberType() throws JacksonException
20622059
}
20632060
return NumberType.BIG_INTEGER;
20642061
}
2065-
20662062
/* And then floating point types. Here optimal type
20672063
* needs to be big decimal, to avoid losing any data?
20682064
* However... using BD is slow, so let's allow returning
20692065
* double as type if no explicit call has been made to access
20702066
* data as BD?
20712067
*/
2072-
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
2073-
return NumberType.BIG_DECIMAL;
2074-
}
2075-
if ((_numTypesValid & NR_DOUBLE) != 0) {
2076-
return NumberType.DOUBLE;
2068+
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
2069+
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
2070+
return NumberType.BIG_DECIMAL;
2071+
}
2072+
if ((_numTypesValid & NR_DOUBLE) != 0) {
2073+
return NumberType.DOUBLE;
2074+
}
2075+
return NumberType.FLOAT;
20772076
}
2078-
return NumberType.FLOAT;
2077+
return null;
20792078
}
20802079

2081-
@Override // since 2.17
2080+
@Override
20822081
public NumberTypeFP getNumberTypeFP() throws JacksonException
20832082
{
20842083
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
@@ -2106,11 +2105,9 @@ public float getFloatValue() throws JacksonException
21062105
{
21072106
if ((_numTypesValid & NR_FLOAT) == 0) {
21082107
if (_numTypesValid == NR_UNKNOWN) {
2109-
_checkNumericValue(NR_FLOAT);
2110-
}
2111-
if ((_numTypesValid & NR_FLOAT) == 0) {
2112-
convertNumberToFloat();
2108+
_checkNumericValue();
21132109
}
2110+
convertNumberToFloat();
21142111
}
21152112
// Bounds/range checks would be tricky here, so let's not bother even trying...
21162113
/*
@@ -2144,13 +2141,12 @@ protected int _parseIntValue() throws JacksonException {
21442141
/**********************************************************************
21452142
*/
21462143

2147-
protected void _checkNumericValue(int expType) throws JacksonException
2144+
protected void _checkNumericValue() throws JacksonException
21482145
{
21492146
// Int or float?
2150-
if (_currToken == JsonToken.VALUE_NUMBER_INT || _currToken == JsonToken.VALUE_NUMBER_FLOAT) {
2151-
return;
2147+
if (_currToken != JsonToken.VALUE_NUMBER_INT && _currToken != JsonToken.VALUE_NUMBER_FLOAT) {
2148+
_reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors");
21522149
}
2153-
_reportError("Current token ("+currentToken()+") not numeric, can not use numeric value accessors");
21542150
}
21552151

21562152
@Override // due to addition of Float as type

cbor/src/test/java/tools/jackson/dataformat/cbor/parse/CBORNumberParsingGetType1433Test.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import org.junit.jupiter.api.Test;
44

55
import tools.jackson.core.*;
6-
import tools.jackson.core.exc.StreamReadException;
76
import tools.jackson.dataformat.cbor.CBORFactory;
87
import tools.jackson.dataformat.cbor.CBORTestBase;
98

109
import static org.junit.jupiter.api.Assertions.assertEquals;
1110
import static org.junit.jupiter.api.Assertions.assertNull;
12-
import static org.junit.jupiter.api.Assertions.fail;
1311

1412
public class CBORNumberParsingGetType1433Test
1513
extends CBORTestBase
@@ -59,12 +57,8 @@ void getNumberType() throws Exception
5957

6058
private void _verifyGetNumberTypeFail(JsonParser p, String token) throws Exception
6159
{
62-
try {
63-
p.getNumberType();
64-
fail("Should not pass");
65-
} catch (StreamReadException e) {
66-
verifyException(e, "Current token ("+token+") not numeric, can not use numeric");
67-
}
60+
// In 2.x got exception; in 3.x null
61+
assertNull(p.getNumberType());
6862
}
6963

7064
private CBORFactory jsonFactory() {

0 commit comments

Comments
 (0)