Skip to content

Commit eaa5f62

Browse files
committed
Complete conversions
x
1 parent 951f884 commit eaa5f62

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

avro/src/main/java/tools/jackson/dataformat/avro/deser/AvroParserImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ protected final void _checkNumericValue() throws JacksonException
322322
{
323323
// Int or float?
324324
if (_currToken != JsonToken.VALUE_NUMBER_INT && _currToken != JsonToken.VALUE_NUMBER_FLOAT) {
325-
_reportError("Current token ("+currentToken()+") not numeric, cannot use numeric value accessors");
325+
throw _constructReadException("Current token (%s) not numeric, cannot use numeric value accessors", _currToken);
326326
}
327327
}
328328

@@ -334,7 +334,7 @@ protected final void convertNumberToInt() throws JacksonException
334334
// Let's verify it's lossless conversion by simple roundtrip
335335
int result = (int) _numberLong;
336336
if (((long) result) != _numberLong) {
337-
_reportError("Numeric value ("+getString()+") out of range of int");
337+
_reportError("Numeric value ("+getString()+") out of range of `int`");
338338
}
339339
_numberInt = result;
340340
} else if ((_numTypesValid & NR_BIGINT) != 0) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,8 @@ protected void _checkNumericValue() throws JacksonException
21452145
{
21462146
// Int or float?
21472147
if (_currToken != JsonToken.VALUE_NUMBER_INT && _currToken != JsonToken.VALUE_NUMBER_FLOAT) {
2148-
_reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors");
2148+
throw _constructReadException("Current token (%s) not numeric, cannot use numeric value accessors",
2149+
_currToken);
21492150
}
21502151
}
21512152

protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ public boolean isNaN() {
15971597
public Number getNumberValue() throws JacksonException
15981598
{
15991599
if (_numTypesValid == NR_UNKNOWN) {
1600-
_checkNumericValue(NR_UNKNOWN); // will also check event type
1600+
_checkNumericValue(); // will also check event type
16011601
}
16021602
// Separate types for int types
16031603
if (_currToken == JsonToken.VALUE_NUMBER_INT) {
@@ -1636,9 +1636,6 @@ public final Number getNumberValueExact() throws JacksonException {
16361636
@Override
16371637
public NumberType getNumberType() throws JacksonException
16381638
{
1639-
if (_numTypesValid == NR_UNKNOWN) {
1640-
_checkNumericValue(NR_UNKNOWN); // will also check event type
1641-
}
16421639
if (_currToken == JsonToken.VALUE_NUMBER_INT) {
16431640
if ((_numTypesValid & NR_LONG) != 0) {
16441641
return NumberType.LONG;
@@ -1655,13 +1652,17 @@ public NumberType getNumberType() throws JacksonException
16551652
* double as type if no explicit call has been made to access
16561653
* data as BD?
16571654
*/
1658-
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
1659-
return NumberType.BIG_DECIMAL;
1660-
}
1661-
if ((_numTypesValid & NR_DOUBLE) != 0) {
1662-
return NumberType.DOUBLE;
1655+
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
1656+
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
1657+
return NumberType.BIG_DECIMAL;
1658+
}
1659+
if ((_numTypesValid & NR_DOUBLE) != 0) {
1660+
return NumberType.DOUBLE;
1661+
}
1662+
return NumberType.FLOAT;
16631663
}
1664-
return NumberType.FLOAT;
1664+
1665+
return null;
16651666
}
16661667

16671668
@Override // since 2.17
@@ -1686,11 +1687,9 @@ public int getIntValue() throws JacksonException
16861687
{
16871688
if ((_numTypesValid & NR_INT) == 0) {
16881689
if (_numTypesValid == NR_UNKNOWN) { // not parsed at all
1689-
_checkNumericValue(NR_INT); // will also check event type
1690-
}
1691-
if ((_numTypesValid & NR_INT) == 0) { // wasn't an int natively?
1692-
convertNumberToInt(); // let's make it so, if possible
1690+
_checkNumericValue(); // will also check event type
16931691
}
1692+
convertNumberToInt(); // let's make it so, if possible
16941693
}
16951694
return _numberInt;
16961695
}
@@ -1700,11 +1699,9 @@ public long getLongValue() throws JacksonException
17001699
{
17011700
if ((_numTypesValid & NR_LONG) == 0) {
17021701
if (_numTypesValid == NR_UNKNOWN) {
1703-
_checkNumericValue(NR_LONG);
1704-
}
1705-
if ((_numTypesValid & NR_LONG) == 0) {
1706-
convertNumberToLong();
1702+
_checkNumericValue();
17071703
}
1704+
convertNumberToLong();
17081705
}
17091706
return _numberLong;
17101707
}
@@ -1714,11 +1711,9 @@ public BigInteger getBigIntegerValue() throws JacksonException
17141711
{
17151712
if ((_numTypesValid & NR_BIGINT) == 0) {
17161713
if (_numTypesValid == NR_UNKNOWN) {
1717-
_checkNumericValue(NR_BIGINT);
1718-
}
1719-
if ((_numTypesValid & NR_BIGINT) == 0) {
1720-
convertNumberToBigInteger();
1714+
_checkNumericValue();
17211715
}
1716+
convertNumberToBigInteger();
17221717
}
17231718
return _numberBigInt;
17241719
}
@@ -1728,11 +1723,9 @@ public float getFloatValue() throws JacksonException
17281723
{
17291724
if ((_numTypesValid & NR_FLOAT) == 0) {
17301725
if (_numTypesValid == NR_UNKNOWN) {
1731-
_checkNumericValue(NR_FLOAT);
1732-
}
1733-
if ((_numTypesValid & NR_FLOAT) == 0) {
1734-
convertNumberToFloat();
1726+
_checkNumericValue();
17351727
}
1728+
convertNumberToFloat();
17361729
}
17371730
// Bounds/range checks would be tricky here, so let's not bother even trying...
17381731
/*
@@ -1748,11 +1741,9 @@ public double getDoubleValue() throws JacksonException
17481741
{
17491742
if ((_numTypesValid & NR_DOUBLE) == 0) {
17501743
if (_numTypesValid == NR_UNKNOWN) {
1751-
_checkNumericValue(NR_DOUBLE);
1752-
}
1753-
if ((_numTypesValid & NR_DOUBLE) == 0) {
1754-
convertNumberToDouble();
1744+
_checkNumericValue();
17551745
}
1746+
convertNumberToDouble();
17561747
}
17571748
return _numberDouble;
17581749
}
@@ -1762,11 +1753,9 @@ public BigDecimal getDecimalValue() throws JacksonException
17621753
{
17631754
if ((_numTypesValid & NR_BIGDECIMAL) == 0) {
17641755
if (_numTypesValid == NR_UNKNOWN) {
1765-
_checkNumericValue(NR_BIGDECIMAL);
1766-
}
1767-
if ((_numTypesValid & NR_BIGDECIMAL) == 0) {
1768-
convertNumberToBigDecimal();
1756+
_checkNumericValue();
17691757
}
1758+
convertNumberToBigDecimal();
17701759
}
17711760
return _numberBigDecimal;
17721761
}
@@ -1777,13 +1766,12 @@ public BigDecimal getDecimalValue() throws JacksonException
17771766
/**********************************************************************
17781767
*/
17791768

1780-
protected void _checkNumericValue(int expType) throws JacksonException
1769+
protected void _checkNumericValue() throws JacksonException
17811770
{
17821771
// Int or float?
1783-
if (_currToken == JsonToken.VALUE_NUMBER_INT || _currToken == JsonToken.VALUE_NUMBER_FLOAT) {
1784-
return;
1772+
if (_currToken != JsonToken.VALUE_NUMBER_INT && _currToken != JsonToken.VALUE_NUMBER_FLOAT) {
1773+
throw _constructReadException("Current token (%s) not numeric, cannot use numeric value accessors", _currToken);
17851774
}
1786-
_reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors");
17871775
}
17881776

17891777
protected void convertNumberToInt() throws JacksonException

0 commit comments

Comments
 (0)