@@ -40,11 +40,18 @@ private BigDecimalParser() {}
40
40
* @throws NumberFormatException
41
41
*/
42
42
public static BigDecimal parse (String valueStr ) {
43
- if (valueStr .length () < 500 ) {
44
- return new BigDecimal (valueStr );
43
+ try {
44
+ if (valueStr .length () < 500 ) {
45
+ return new BigDecimal (valueStr );
46
+ }
47
+ // workaround https://github.com/FasterXML/jackson-databind/issues/4694
48
+ return JavaBigDecimalParser .parseBigDecimal (valueStr );
49
+
50
+ // 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException
51
+ // operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both:
52
+ } catch (ArithmeticException | NumberFormatException e ) {
53
+ throw _parseFailure (e , valueStr );
45
54
}
46
- // workaround https://github.com/FasterXML/jackson-databind/issues/4694
47
- return JavaBigDecimalParser .parseBigDecimal (valueStr );
48
55
}
49
56
50
57
/**
@@ -68,7 +75,7 @@ public static BigDecimal parse(final char[] chars, final int off, final int len)
68
75
// 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException
69
76
// operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both:
70
77
} catch (ArithmeticException | NumberFormatException e ) {
71
- throw _parseFailure (e , new String ( chars , off , len ) );
78
+ throw _parseFailure (e , chars , off , len );
72
79
}
73
80
}
74
81
@@ -118,7 +125,8 @@ public static BigDecimal parseWithFastParser(final String valueStr) {
118
125
*/
119
126
public static BigDecimal parseWithFastParser (final char [] ch , final int off , final int len ) {
120
127
try {
121
- return JavaBigDecimalParser .parseBigDecimal (ch , off , len );
128
+ // workaround https://github.com/FasterXML/jackson-databind/issues/4694
129
+ return JavaBigDecimalParser .parseBigDecimal (new String (ch , off , len ));
122
130
} catch (ArithmeticException | NumberFormatException e ) {
123
131
throw _parseFailure (e , new String (ch , off , len ));
124
132
}
@@ -131,11 +139,23 @@ private static NumberFormatException _parseFailure(Exception e, String fullValue
131
139
desc = "Not a valid number representation" ;
132
140
}
133
141
String valueToReport = _getValueDesc (fullValue );
134
- return new NumberFormatException ("Value " + valueToReport
135
- + " can not be deserialized as `java.math.BigDecimal`, reason: " + desc );
142
+ return new NumberFormatException (_generateExceptionMessage (valueToReport , desc ));
143
+ }
144
+
145
+ private static NumberFormatException _parseFailure (final Exception e ,
146
+ final char [] array ,
147
+ final int offset ,
148
+ final int len ) {
149
+ String desc = e .getMessage ();
150
+ // 05-Feb-2021, tatu: Alas, JDK mostly has null message so:
151
+ if (desc == null ) {
152
+ desc = "Not a valid number representation" ;
153
+ }
154
+ String valueToReport = _getValueDesc (array , offset , len );
155
+ return new NumberFormatException (_generateExceptionMessage (valueToReport , desc ));
136
156
}
137
157
138
- private static String _getValueDesc (String fullValue ) {
158
+ private static String _getValueDesc (final String fullValue ) {
139
159
final int len = fullValue .length ();
140
160
if (len <= MAX_CHARS_TO_REPORT ) {
141
161
return String .format ("\" %s\" " , fullValue );
@@ -145,4 +165,18 @@ private static String _getValueDesc(String fullValue) {
145
165
MAX_CHARS_TO_REPORT , len );
146
166
}
147
167
168
+ private static String _getValueDesc (final char [] array , final int offset , final int len ) {
169
+ if (len <= MAX_CHARS_TO_REPORT ) {
170
+ return String .format ("\" %s\" " , new String (array , offset , len ));
171
+ }
172
+ return String .format ("\" %s\" (truncated to %d chars (from %d))" ,
173
+ new String (array , offset , MAX_CHARS_TO_REPORT ),
174
+ MAX_CHARS_TO_REPORT , len );
175
+ }
176
+
177
+ private static String _generateExceptionMessage (final String valueToReport , final String desc ) {
178
+ return String .format ("Value %s cannot be deserialized as `java.math.BigDecimal`, reason: %s" ,
179
+ valueToReport , desc );
180
+ }
181
+
148
182
}
0 commit comments