2
2
3
3
import java .math .BigDecimal ;
4
4
import java .math .BigInteger ;
5
+ import java .util .Optional ;
6
+ import java .util .OptionalDouble ;
7
+ import java .util .OptionalInt ;
8
+ import java .util .OptionalLong ;
5
9
6
10
import tools .jackson .core .*;
7
11
import tools .jackson .databind .JacksonSerializable ;
@@ -47,6 +51,172 @@ protected BaseJsonNode() { }
47
51
@ Override
48
52
public boolean isEmbeddedValue () { return false ; }
49
53
54
+ /*
55
+ /**********************************************************************
56
+ /* Defaulting for number access
57
+ /**********************************************************************
58
+ */
59
+
60
+ @ Override
61
+ public Number numberValue () {
62
+ return _reportCoercionFail ("numberValue()" , Number .class , "value type not numeric" );
63
+ }
64
+
65
+ @ Override
66
+ public short shortValue () {
67
+ return _reportCoercionFail ("shortValue()" , Short .TYPE , "value type not numeric" );
68
+ }
69
+
70
+ @ Override
71
+ public int intValue () {
72
+ return _reportCoercionFail ("intValue()" , Integer .TYPE , "value type not numeric" );
73
+ }
74
+
75
+ @ Override
76
+ public int intValue (int defaultValue ) {
77
+ // Overridden by NumericNode, for other types return default
78
+ return defaultValue ;
79
+ }
80
+
81
+ @ Override
82
+ public OptionalInt intValueOpt () {
83
+ // Overridden by NumericNode, for other types return default
84
+ return OptionalInt .empty ();
85
+ }
86
+
87
+ @ Override
88
+ public int asInt () {
89
+ return asInt (0 );
90
+ }
91
+
92
+ @ Override
93
+ public int asInt (int defaultValue ) {
94
+ return defaultValue ;
95
+ }
96
+
97
+ @ Override
98
+ public long longValue () {
99
+ return _reportCoercionFail ("longValue()" , Long .TYPE , "value type not numeric" );
100
+ }
101
+
102
+ @ Override
103
+ public long longValue (long defaultValue ) {
104
+ // Overridden by NumericNode, for other types return default
105
+ return defaultValue ;
106
+ }
107
+
108
+ @ Override
109
+ public OptionalLong longValueOpt () {
110
+ // Overridden by NumericNode, for other types return default
111
+ return OptionalLong .empty ();
112
+ }
113
+
114
+ @ Override
115
+ public long asLong () {
116
+ return asLong (0L );
117
+ }
118
+
119
+ @ Override
120
+ public long asLong (long defaultValue ) {
121
+ return defaultValue ;
122
+ }
123
+
124
+ @ Override
125
+ public BigInteger bigIntegerValue () {
126
+ return _reportCoercionFail ("bigIntegerValue()" , BigInteger .class , "value type not numeric" );
127
+ }
128
+
129
+ @ Override
130
+ public float floatValue () {
131
+ return _reportCoercionFail ("floatValue()" , Float .TYPE , "value type not numeric" );
132
+ }
133
+
134
+ @ Override
135
+ public double doubleValue () {
136
+ return _reportCoercionFail ("doubleValue()" , Double .TYPE , "value type not numeric" );
137
+ }
138
+
139
+ @ Override
140
+ public double doubleValue (double defaultValue ) {
141
+ // Overridden by NumericNode, for other types return default
142
+ return defaultValue ;
143
+ }
144
+
145
+ @ Override
146
+ public OptionalDouble doubleValueOpt () {
147
+ // Overridden by NumericNode, for other types return default
148
+ return OptionalDouble .empty ();
149
+ }
150
+
151
+ @ Override
152
+ public double asDouble () {
153
+ return asDouble (0.0 );
154
+ }
155
+
156
+ @ Override
157
+ public double asDouble (double defaultValue ) {
158
+ return defaultValue ;
159
+ }
160
+
161
+ @ Override
162
+ public BigDecimal decimalValue () {
163
+ return _reportCoercionFail ("decimalValue()" , BigDecimal .class , "value type not numeric" );
164
+ }
165
+
166
+ @ Override
167
+ public BigDecimal decimalValue (BigDecimal defaultValue ) {
168
+ // Overridden by NumericNode, for other types return default
169
+ return defaultValue ;
170
+ }
171
+
172
+ @ Override
173
+ public Optional <BigDecimal > decimalValueOpt () {
174
+ return Optional .empty ();
175
+ }
176
+
177
+ @ Override
178
+ public BigDecimal asDecimal () {
179
+ return asDecimal (BigDecimal .ZERO );
180
+ }
181
+
182
+ @ Override
183
+ public BigDecimal asDecimal (BigDecimal defaultValue ) {
184
+ // !!! TODO
185
+ return decimalValue (defaultValue );
186
+ }
187
+
188
+ /*
189
+ /**********************************************************************
190
+ /* Defaulting for non-number scalar access
191
+ /**********************************************************************
192
+ */
193
+
194
+ @ Override
195
+ public byte [] binaryValue () {
196
+ return null ;
197
+ }
198
+
199
+ @ Override
200
+ public boolean booleanValue () {
201
+ return false ;
202
+ }
203
+
204
+ @ Override
205
+ public boolean asBoolean () {
206
+ return asBoolean (false );
207
+ }
208
+
209
+ @ Override
210
+ public boolean asBoolean (boolean defaultValue ) {
211
+ return defaultValue ;
212
+ }
213
+
214
+ @ Override
215
+ public String asString (String defaultValue ) {
216
+ String str = asString ();
217
+ return (str == null ) ? defaultValue : str ;
218
+ }
219
+
50
220
/*
51
221
/**********************************************************************
52
222
/* Basic definitions for non-container types
@@ -261,6 +431,59 @@ public String toPrettyString() {
261
431
/**********************************************************************
262
432
*/
263
433
434
+ protected <T > T _reportCoercionFail (String method , Class <?> targetType ,
435
+ String message )
436
+ {
437
+ throw JsonNodeException .from (this , "'%s' method `%s` cannot convert value %s to %s: %s" ,
438
+ getClass ().getSimpleName (), method ,
439
+ _valueDesc (), ClassUtil .nameOf (targetType ), message );
440
+ }
441
+
442
+ protected short _reportShortCoercionRangeFail (String method ) {
443
+ return _reportCoercionFail (method , Short .TYPE ,
444
+ "value not in 16-bit `short` range" );
445
+ }
446
+
447
+ protected int _reportIntCoercionRangeFail (String method ) {
448
+ return _reportCoercionFail (method , Integer .TYPE ,
449
+ "value not in 32-bit `int` range" );
450
+ }
451
+
452
+ protected long _reportLongCoercionRangeFail (String method ) {
453
+ return _reportCoercionFail (method , Long .TYPE ,
454
+ "value not in 64-bit `long` range" );
455
+ }
456
+
457
+ protected float _reportFloatCoercionRangeFail (String method ) {
458
+ return _reportCoercionFail (method , Float .TYPE ,
459
+ "value not in 32-bit `float` range" );
460
+ }
461
+
462
+ protected double _reportDoubleCoercionRangeFail (String method ) {
463
+ return _reportCoercionFail (method , Double .TYPE ,
464
+ "value not in 64-bit `double` range" );
465
+ }
466
+
467
+ protected short _reportShortCoercionFractionFail (String method ) {
468
+ return _reportCoercionFail (method , Short .TYPE ,
469
+ "value has fractional part" );
470
+ }
471
+
472
+ protected int _reportIntCoercionFractionFail (String method ) {
473
+ return _reportCoercionFail (method , Integer .TYPE ,
474
+ "value has fractional part" );
475
+ }
476
+
477
+ protected long _reportLongCoercionFractionFail (String method ) {
478
+ return _reportCoercionFail (method , Long .TYPE ,
479
+ "value has fractional part" );
480
+ }
481
+
482
+ protected BigInteger _reportBigIntegerCoercionFractionFail (String method ) {
483
+ return _reportCoercionFail (method , BigInteger .class ,
484
+ "value has fractional part" );
485
+ }
486
+
264
487
/**
265
488
* Helper method that throws {@link JsonNodeException} as a result of
266
489
* this node being of wrong type
@@ -275,15 +498,16 @@ protected <T> T _reportWrongNodeType(String msgTemplate, Object...args) {
275
498
/**********************************************************************
276
499
*/
277
500
278
- protected BigInteger _bigIntFromBigDec (BigDecimal value ) {
279
- StreamReadConstraints .defaults ().validateBigIntegerScale (value .scale ());
280
- return value .toBigInteger ();
281
- }
282
-
283
501
protected JsonPointer _jsonPointerIfValid (String exprOrProperty ) {
284
502
if (exprOrProperty .isEmpty () || exprOrProperty .charAt (0 ) == '/' ) {
285
503
return JsonPointer .compile (exprOrProperty );
286
504
}
287
505
return null ;
288
506
}
507
+
508
+ /**
509
+ * Method for implementation classes to return a short description of contained
510
+ * value, to be used in error messages.
511
+ */
512
+ protected abstract String _valueDesc ();
289
513
}
0 commit comments