11package it .aboutbits .springboot .toolbox .jackson ;
22
3- import com .fasterxml .jackson .core .JsonParser ;
4- import com .fasterxml .jackson .databind .DeserializationContext ;
5- import com .fasterxml .jackson .databind .JsonDeserializer ;
63import it .aboutbits .springboot .toolbox .reflection .util .CustomTypeReflectionUtil ;
74import it .aboutbits .springboot .toolbox .type .CustomType ;
85import it .aboutbits .springboot .toolbox .type .ScaledBigDecimal ;
6+ import tools .jackson .core .JacksonException ;
7+ import tools .jackson .core .JsonParser ;
8+ import tools .jackson .core .TokenStreamLocation ;
9+ import tools .jackson .core .exc .InputCoercionException ;
10+ import tools .jackson .databind .DeserializationContext ;
11+ import tools .jackson .databind .ValueDeserializer ;
912
10- import java .io .IOException ;
13+ import java .io .Closeable ;
1114import java .lang .reflect .Constructor ;
1215import java .lang .reflect .InvocationTargetException ;
1316import java .math .BigDecimal ;
1417import java .math .BigInteger ;
1518import java .util .UUID ;
1619import java .util .function .Function ;
1720
18- public class CustomTypeDeserializer <T extends CustomType <?>> extends JsonDeserializer <T > {
21+ public class CustomTypeDeserializer <T extends CustomType <?>> extends ValueDeserializer <T > {
1922 private final Class <T > customType ;
2023 private final Constructor <T > constructor ;
2124 private final Function <JsonParser , Object > typeConverter ;
@@ -43,7 +46,7 @@ public Class<T> handledType() {
4346 }
4447
4548 @ Override
46- public T deserialize (JsonParser jsonParser , DeserializationContext deserializationContext ) throws IOException {
49+ public T deserialize (JsonParser jsonParser , DeserializationContext deserializationContext ) {
4750 var value = typeConverter .apply (jsonParser );
4851
4952 try {
@@ -52,7 +55,46 @@ public T deserialize(JsonParser jsonParser, DeserializationContext deserializati
5255 IllegalAccessException
5356 | InvocationTargetException
5457 | InstantiationException e ) {
55- throw new IOException (e );
58+ throw new Ex (e );
59+ }
60+ }
61+
62+ public static class Ex extends JacksonException {
63+
64+ protected Ex (String msg ) {
65+ super (msg );
66+ }
67+
68+ protected Ex (Throwable rootCause ) {
69+ super (rootCause );
70+ }
71+
72+ protected Ex (String msg , Throwable rootCause ) {
73+ super (msg , rootCause );
74+ }
75+
76+ protected Ex (String msg , TokenStreamLocation loc , Throwable rootCause ) {
77+ super (msg , loc , rootCause );
78+ }
79+
80+ protected Ex (Closeable processor , Throwable rootCause ) {
81+ super (processor , rootCause );
82+ }
83+
84+ protected Ex (Closeable processor , String msg , TokenStreamLocation loc , Throwable rootCause ) {
85+ super (processor , msg , loc , rootCause );
86+ }
87+
88+ protected Ex (Closeable processor , String msg ) {
89+ super (processor , msg );
90+ }
91+
92+ protected Ex (Closeable processor , String msg , Throwable problem ) {
93+ super (processor , msg , problem );
94+ }
95+
96+ protected Ex (Closeable processor , String msg , TokenStreamLocation loc ) {
97+ super (processor , msg , loc );
5698 }
5799 }
58100
@@ -106,7 +148,7 @@ private static Function<JsonParser, Object> getByteConverter() {
106148 return jsonParser -> {
107149 try {
108150 return jsonParser .getByteValue ();
109- } catch (IOException e ) {
151+ } catch (InputCoercionException e ) {
110152 throw new CustomTypeDeserializerException ("Failed to read value as Byte." , e );
111153 }
112154 };
@@ -116,7 +158,7 @@ private static Function<JsonParser, Object> getBooleanConverter() {
116158 return jsonParser -> {
117159 try {
118160 return jsonParser .getBooleanValue ();
119- } catch (IOException e ) {
161+ } catch (InputCoercionException e ) {
120162 throw new CustomTypeDeserializerException ("Failed to read value as Boolean." , e );
121163 }
122164 };
@@ -126,7 +168,7 @@ private static Function<JsonParser, Object> getScaledBigDecimalConverter() {
126168 return jsonParser -> {
127169 try {
128170 return new ScaledBigDecimal (jsonParser .getDecimalValue ());
129- } catch (IOException e ) {
171+ } catch (InputCoercionException e ) {
130172 throw new CustomTypeDeserializerException ("Failed to read value as ScaledBigDecimal." , e );
131173 }
132174 };
@@ -136,7 +178,7 @@ private static Function<JsonParser, Object> getBigDecimalConverter() {
136178 return jsonParser -> {
137179 try {
138180 return jsonParser .getDecimalValue ();
139- } catch (IOException e ) {
181+ } catch (InputCoercionException e ) {
140182 throw new CustomTypeDeserializerException ("Failed to read value as BigDecimal." , e );
141183 }
142184 };
@@ -146,7 +188,7 @@ private static Function<JsonParser, Object> getDoubleConverter() {
146188 return jsonParser -> {
147189 try {
148190 return jsonParser .getDoubleValue ();
149- } catch (IOException e ) {
191+ } catch (InputCoercionException e ) {
150192 throw new CustomTypeDeserializerException ("Failed to read value as Double." , e );
151193 }
152194 };
@@ -156,7 +198,7 @@ private static Function<JsonParser, Object> getFloatConverter() {
156198 return jsonParser -> {
157199 try {
158200 return jsonParser .getFloatValue ();
159- } catch (IOException e ) {
201+ } catch (InputCoercionException e ) {
160202 throw new CustomTypeDeserializerException ("Failed to read value as Float." , e );
161203 }
162204 };
@@ -166,7 +208,7 @@ private static Function<JsonParser, Object> getBigIntegerConverter() {
166208 return jsonParser -> {
167209 try {
168210 return jsonParser .getBigIntegerValue ();
169- } catch (IOException e ) {
211+ } catch (InputCoercionException e ) {
170212 throw new CustomTypeDeserializerException ("Failed to read value as BigInteger." , e );
171213 }
172214 };
@@ -176,7 +218,7 @@ private static Function<JsonParser, Object> getLongConverter() {
176218 return jsonParser -> {
177219 try {
178220 return jsonParser .getLongValue ();
179- } catch (IOException e ) {
221+ } catch (InputCoercionException e ) {
180222 throw new CustomTypeDeserializerException ("Failed to read value as Long." , e );
181223 }
182224 };
@@ -186,7 +228,7 @@ private static Function<JsonParser, Object> getIntegerConverter() {
186228 return jsonParser -> {
187229 try {
188230 return jsonParser .getIntValue ();
189- } catch (IOException e ) {
231+ } catch (InputCoercionException e ) {
190232 throw new CustomTypeDeserializerException ("Failed to read value as Integer." , e );
191233 }
192234 };
@@ -196,7 +238,7 @@ private static Function<JsonParser, Object> getShortConverter() {
196238 return jsonParser -> {
197239 try {
198240 return jsonParser .getShortValue ();
199- } catch (IOException e ) {
241+ } catch (InputCoercionException e ) {
200242 throw new CustomTypeDeserializerException ("Failed to read value as Short." , e );
201243 }
202244 };
@@ -206,7 +248,7 @@ private static Function<JsonParser, Object> getStringConverter() {
206248 return jsonParser -> {
207249 try {
208250 return jsonParser .getValueAsString ();
209- } catch (IOException e ) {
251+ } catch (InputCoercionException e ) {
210252 throw new CustomTypeDeserializerException ("Failed to read value as String." , e );
211253 }
212254 };
@@ -217,10 +259,15 @@ private static Function<JsonParser, Object> getCharConverter() {
217259 try {
218260 var value = jsonParser .getValueAsString ();
219261 if (value == null || value .length () != 1 ) {
220- throw new IOException ();
262+ throw new InputCoercionException (
263+ jsonParser ,
264+ "Not a Char." ,
265+ jsonParser .currentToken (),
266+ String .class
267+ );
221268 }
222269 return value .charAt (0 );
223- } catch (IOException e ) {
270+ } catch (InputCoercionException e ) {
224271 throw new CustomTypeDeserializerException ("Failed to read value as Char." , e );
225272 }
226273 };
@@ -231,10 +278,15 @@ private static Function<JsonParser, Object> getUUIDConverter() {
231278 try {
232279 var value = jsonParser .getValueAsString ();
233280 if (value == null || value .length () != 36 ) {
234- throw new IOException ();
281+ throw new InputCoercionException (
282+ jsonParser ,
283+ "Not a UUID." ,
284+ jsonParser .currentToken (),
285+ String .class
286+ );
235287 }
236288 return UUID .fromString (value );
237- } catch (IOException e ) {
289+ } catch (InputCoercionException e ) {
238290 throw new CustomTypeDeserializerException ("Failed to read value as UUID." , e );
239291 }
240292 };
@@ -255,7 +307,7 @@ private static Function<JsonParser, Object> getEnumConverter(Class<?> wrappedTyp
255307 "Failed to read value as Enum: " + enumClass .getName (),
256308 e
257309 );
258- } catch (IOException e ) {
310+ } catch (InputCoercionException e ) {
259311 throw new CustomTypeDeserializerException ("Failed to read value as Enum." , e );
260312 }
261313 };
0 commit comments