@@ -207,7 +207,7 @@ public abstract class ParserBase extends ParserMinimalBase
207
207
* Textual number representation captured from input in cases lazy-parsing
208
208
* is desired.
209
209
*<p>
210
- * As of 2.14 this only applies to {@link BigInteger}.
210
+ * As of 2.14, this only applies to {@link BigInteger} and {@link BigDecimal }.
211
211
*
212
212
* @since 2.14
213
213
*/
@@ -625,7 +625,7 @@ public Number getNumberValue() throws IOException
625
625
// And then floating point types. But here optimal type
626
626
// needs to be big decimal, to avoid losing any data?
627
627
if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
628
- return _numberBigDecimal ;
628
+ return _getBigDecimal () ;
629
629
}
630
630
if ((_numTypesValid & NR_FLOAT ) != 0 ) {
631
631
return _numberFloat ;
@@ -660,7 +660,7 @@ public Number getNumberValueExact() throws IOException
660
660
_parseNumericValue (NR_BIGDECIMAL );
661
661
}
662
662
if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
663
- return _numberBigDecimal ;
663
+ return _getBigDecimal () ;
664
664
}
665
665
if ((_numTypesValid & NR_FLOAT ) != 0 ) {
666
666
return _numberFloat ;
@@ -791,7 +791,7 @@ public BigDecimal getDecimalValue() throws IOException
791
791
convertNumberToBigDecimal ();
792
792
}
793
793
}
794
- return _numberBigDecimal ;
794
+ return _getBigDecimal () ;
795
795
}
796
796
797
797
/*
@@ -900,7 +900,8 @@ private void _parseSlowFloat(int expType) throws IOException
900
900
*/
901
901
try {
902
902
if (expType == NR_BIGDECIMAL ) {
903
- _numberBigDecimal = _textBuffer .contentsAsDecimal ();
903
+ _numberBigDecimal = null ;
904
+ _numberString = _textBuffer .contentsAsString ();
904
905
_numTypesValid = NR_BIGDECIMAL ;
905
906
} else if (expType == NR_FLOAT ) {
906
907
_numberFloat = _textBuffer .contentsAsFloat (isEnabled (Feature .USE_FAST_DOUBLE_PARSER ));
@@ -992,11 +993,12 @@ protected void convertNumberToInt() throws IOException
992
993
}
993
994
_numberInt = (int ) _numberDouble ;
994
995
} else if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
995
- if (BD_MIN_INT .compareTo (_numberBigDecimal ) > 0
996
- || BD_MAX_INT .compareTo (_numberBigDecimal ) < 0 ) {
996
+ final BigDecimal bigDecimal = _getBigDecimal ();
997
+ if (BD_MIN_INT .compareTo (bigDecimal ) > 0
998
+ || BD_MAX_INT .compareTo (bigDecimal ) < 0 ) {
997
999
reportOverflowInt ();
998
1000
}
999
- _numberInt = _numberBigDecimal .intValue ();
1001
+ _numberInt = bigDecimal .intValue ();
1000
1002
} else {
1001
1003
_throwInternal ();
1002
1004
}
@@ -1021,11 +1023,12 @@ protected void convertNumberToLong() throws IOException
1021
1023
}
1022
1024
_numberLong = (long ) _numberDouble ;
1023
1025
} else if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
1024
- if (BD_MIN_LONG .compareTo (_numberBigDecimal ) > 0
1025
- || BD_MAX_LONG .compareTo (_numberBigDecimal ) < 0 ) {
1026
+ final BigDecimal bigDecimal = _getBigDecimal ();
1027
+ if (BD_MIN_LONG .compareTo (bigDecimal ) > 0
1028
+ || BD_MAX_LONG .compareTo (bigDecimal ) < 0 ) {
1026
1029
reportOverflowLong ();
1027
1030
}
1028
- _numberLong = _numberBigDecimal .longValue ();
1031
+ _numberLong = bigDecimal .longValue ();
1029
1032
} else {
1030
1033
_throwInternal ();
1031
1034
}
@@ -1036,7 +1039,7 @@ protected void convertNumberToBigInteger() throws IOException
1036
1039
{
1037
1040
if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
1038
1041
// here it'll just get truncated, no exceptions thrown
1039
- _numberBigInt = _numberBigDecimal .toBigInteger ();
1042
+ _numberBigInt = _getBigDecimal () .toBigInteger ();
1040
1043
} else if ((_numTypesValid & NR_LONG ) != 0 ) {
1041
1044
_numberBigInt = BigInteger .valueOf (_numberLong );
1042
1045
} else if ((_numTypesValid & NR_INT ) != 0 ) {
@@ -1058,7 +1061,7 @@ protected void convertNumberToDouble() throws IOException
1058
1061
*/
1059
1062
1060
1063
if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
1061
- _numberDouble = _numberBigDecimal .doubleValue ();
1064
+ _numberDouble = _getBigDecimal () .doubleValue ();
1062
1065
} else if ((_numTypesValid & NR_BIGINT ) != 0 ) {
1063
1066
_numberDouble = _getBigInteger ().doubleValue ();
1064
1067
} else if ((_numTypesValid & NR_LONG ) != 0 ) {
@@ -1082,7 +1085,7 @@ protected void convertNumberToFloat() throws IOException
1082
1085
*/
1083
1086
1084
1087
if ((_numTypesValid & NR_BIGDECIMAL ) != 0 ) {
1085
- _numberFloat = _numberBigDecimal .floatValue ();
1088
+ _numberFloat = _getBigDecimal () .floatValue ();
1086
1089
} else if ((_numTypesValid & NR_BIGINT ) != 0 ) {
1087
1090
_numberFloat = _getBigInteger ().floatValue ();
1088
1091
} else if ((_numTypesValid & NR_LONG ) != 0 ) {
@@ -1137,6 +1140,23 @@ protected BigInteger _getBigInteger() {
1137
1140
_numberString = null ;
1138
1141
return _numberBigInt ;
1139
1142
}
1143
+
1144
+ /**
1145
+ * Internal accessor that needs to be used for accessing number value of type
1146
+ * {@link BigDecimal} which -- as of 2.14 -- is typically lazily parsed.
1147
+ *
1148
+ * @since 2.14
1149
+ */
1150
+ protected BigDecimal _getBigDecimal () {
1151
+ if (_numberBigDecimal != null ) {
1152
+ return _numberBigDecimal ;
1153
+ } else if (_numberString == null ) {
1154
+ throw new IllegalStateException ("cannot get BigDecimal from current parser state" );
1155
+ }
1156
+ _numberBigDecimal = NumberInput .parseBigDecimal (_numberString );
1157
+ _numberString = null ;
1158
+ return _numberBigDecimal ;
1159
+ }
1140
1160
1141
1161
/*
1142
1162
/**********************************************************
@@ -1374,4 +1394,5 @@ protected void loadMoreGuaranteed() throws IOException {
1374
1394
1375
1395
// Can't declare as deprecated, for now, but shouldn't be needed
1376
1396
protected void _finishString () throws IOException { }
1397
+
1377
1398
}
0 commit comments