@@ -1012,7 +1012,11 @@ public Object handleMissingInstantiator(Class<?> instClass, JsonParser p,
1012
1012
}
1013
1013
h = h .next ();
1014
1014
}
1015
- throw instantiationException (instClass , msg );
1015
+ // 06-Oct-2016, tatu: This is input mismatch problem, in that absence of
1016
+ // creator implies that incoming content type or structure is wrong
1017
+ msg = String .format ("Can not construct instance of %s, problem: %s" ,
1018
+ instClass .getName (), msg );
1019
+ return reportInputMismatch (instClass , msg );
1016
1020
}
1017
1021
1018
1022
/**
@@ -1108,9 +1112,9 @@ public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
1108
1112
if ((instance == null ) || instClass .isInstance (instance )) {
1109
1113
return instance ;
1110
1114
}
1111
- reportInputMismatch (
1115
+ reportInputMismatch (instClass ,
1112
1116
"DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s" ,
1113
- instClass , instance .getClass ());
1117
+ instance .getClass ());
1114
1118
}
1115
1119
h = h .next ();
1116
1120
}
@@ -1123,7 +1127,7 @@ public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
1123
1127
_calcName (instClass ), t );
1124
1128
}
1125
1129
}
1126
- reportInputMismatch (msg );
1130
+ reportInputMismatch (instClass , msg );
1127
1131
return null ; // never gets here
1128
1132
}
1129
1133
@@ -1189,8 +1193,59 @@ public JavaType handleUnknownTypeId(JavaType baseType, String id,
1189
1193
* recovery is attempted (via {@link DeserializationProblemHandler}, as
1190
1194
* problem is considered to be difficult to recover from, in general.
1191
1195
*
1192
- * @since 2.8
1196
+ * @since 2.9
1197
+ */
1198
+ public void reportWrongTokenException (JsonDeserializer <?> deser ,
1199
+ JsonToken expToken , String msg , Object ... msgArgs )
1200
+ throws JsonMappingException
1201
+ {
1202
+ if ((msg != null ) && (msgArgs .length > 0 )) {
1203
+ msg = String .format (msg , msgArgs );
1204
+ }
1205
+ throw wrongTokenException (getParser (), deser .handledType (), expToken , msg );
1206
+ }
1207
+
1208
+ /**
1209
+ * Method for deserializers to call
1210
+ * when the token encountered was of type different than what <b>should</b>
1211
+ * be seen at that position, usually within a sequence of expected tokens.
1212
+ * Note that this method will throw a {@link JsonMappingException} and no
1213
+ * recovery is attempted (via {@link DeserializationProblemHandler}, as
1214
+ * problem is considered to be difficult to recover from, in general.
1215
+ *
1216
+ * @since 2.9
1193
1217
*/
1218
+ public void reportWrongTokenException (JavaType targetType ,
1219
+ JsonToken expToken , String msg , Object ... msgArgs )
1220
+ throws JsonMappingException
1221
+ {
1222
+ if ((msg != null ) && (msgArgs .length > 0 )) {
1223
+ msg = String .format (msg , msgArgs );
1224
+ }
1225
+ throw wrongTokenException (getParser (), targetType , expToken , msg );
1226
+ }
1227
+
1228
+ /**
1229
+ * Method for deserializers to call
1230
+ * when the token encountered was of type different than what <b>should</b>
1231
+ * be seen at that position, usually within a sequence of expected tokens.
1232
+ * Note that this method will throw a {@link JsonMappingException} and no
1233
+ * recovery is attempted (via {@link DeserializationProblemHandler}, as
1234
+ * problem is considered to be difficult to recover from, in general.
1235
+ *
1236
+ * @since 2.9
1237
+ */
1238
+ public void reportWrongTokenException (Class <?> targetType ,
1239
+ JsonToken expToken , String msg , Object ... msgArgs )
1240
+ throws JsonMappingException
1241
+ {
1242
+ if ((msg != null ) && (msgArgs .length > 0 )) {
1243
+ msg = String .format (msg , msgArgs );
1244
+ }
1245
+ throw wrongTokenException (getParser (), targetType , expToken , msg );
1246
+ }
1247
+
1248
+ @ Deprecated // since 2.9
1194
1249
public void reportWrongTokenException (JsonParser p ,
1195
1250
JsonToken expToken , String msg , Object ... msgArgs )
1196
1251
throws JsonMappingException
@@ -1228,7 +1283,10 @@ public void reportUnknownProperty(Object instanceOrClass, String fieldName,
1228
1283
1229
1284
/**
1230
1285
* @since 2.8
1286
+ *
1287
+ * @deprecated Since 2.9: not clear this ever occurs
1231
1288
*/
1289
+ @ Deprecated // since 2.9
1232
1290
public void reportMissingContent (String msg , Object ... msgArgs )
1233
1291
throws JsonMappingException
1234
1292
{
@@ -1237,7 +1295,7 @@ public void reportMissingContent(String msg, Object... msgArgs)
1237
1295
} else if (msgArgs .length > 0 ) {
1238
1296
msg = String .format (msg , msgArgs );
1239
1297
}
1240
- throw InputMismatchException .from (getParser (), msg );
1298
+ throw InputMismatchException .from (getParser (), ( JavaType ) null , msg );
1241
1299
}
1242
1300
1243
1301
/**
@@ -1257,10 +1315,13 @@ public <T> T reportUnresolvedObjectId(ObjectIdReader oidReader, Object bean)
1257
1315
*
1258
1316
* @since 2.9
1259
1317
*/
1260
- public <T > T reportInputMismatch (BeanProperty prop , String msg )
1261
- throws JsonMappingException
1318
+ public <T > T reportInputMismatch (BeanProperty prop ,
1319
+ String msg , Object ... msgArgs ) throws JsonMappingException
1262
1320
{
1263
- throw InputMismatchException .from (getParser (), msg );
1321
+ if (msgArgs .length > 0 ) {
1322
+ msg = String .format (msg , msgArgs );
1323
+ }
1324
+ throw InputMismatchException .from (getParser (), prop .getType (), msg );
1264
1325
}
1265
1326
1266
1327
/**
@@ -1269,10 +1330,13 @@ public <T> T reportInputMismatch(BeanProperty prop, String msg)
1269
1330
*
1270
1331
* @since 2.9
1271
1332
*/
1272
- public <T > T reportInputMismatch (JsonDeserializer <?> src , String msg )
1273
- throws JsonMappingException
1333
+ public <T > T reportInputMismatch (JsonDeserializer <?> src ,
1334
+ String msg , Object ... msgArgs ) throws JsonMappingException
1274
1335
{
1275
- throw InputMismatchException .from (getParser (), msg );
1336
+ if (msgArgs .length > 0 ) {
1337
+ msg = String .format (msg , msgArgs );
1338
+ }
1339
+ throw InputMismatchException .from (getParser (), src .handledType (), msg );
1276
1340
}
1277
1341
1278
1342
/**
@@ -1281,14 +1345,30 @@ public <T> T reportInputMismatch(JsonDeserializer<?> src, String msg)
1281
1345
*
1282
1346
* @since 2.9
1283
1347
*/
1284
- public <T > T reportInputMismatch (String msg , Object ... msgArgs ) throws JsonMappingException
1348
+ public <T > T reportInputMismatch (Class <?> targetType ,
1349
+ String msg , Object ... msgArgs ) throws JsonMappingException
1285
1350
{
1286
1351
if (msgArgs .length > 0 ) {
1287
1352
msg = String .format (msg , msgArgs );
1288
1353
}
1289
- throw InputMismatchException .from (getParser (), msg );
1354
+ throw InputMismatchException .from (getParser (), targetType , msg );
1290
1355
}
1291
1356
1357
+ /**
1358
+ * Helper method used to indicate a problem with input in cases where more
1359
+ * specific <code>reportXxx()</code> method was not available.
1360
+ *
1361
+ * @since 2.9
1362
+ */
1363
+ public <T > T reportInputMismatch (JavaType targetType ,
1364
+ String msg , Object ... msgArgs ) throws JsonMappingException
1365
+ {
1366
+ if (msgArgs .length > 0 ) {
1367
+ msg = String .format (msg , msgArgs );
1368
+ }
1369
+ throw InputMismatchException .from (getParser (), targetType , msg );
1370
+ }
1371
+
1292
1372
/*
1293
1373
/**********************************************************
1294
1374
/* Methods for problem reporting, in cases where recovery
@@ -1352,16 +1432,36 @@ public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingEx
1352
1432
* Note that most of the time this method should NOT be directly called;
1353
1433
* instead, {@link #reportWrongTokenException} should be called and will
1354
1434
* call this method as necessary.
1435
+ *
1436
+ * @since 2.9
1355
1437
*/
1356
- public JsonMappingException wrongTokenException (JsonParser p , JsonToken expToken ,
1357
- String msg0 )
1438
+ public JsonMappingException wrongTokenException (JsonParser p , JavaType targetType ,
1439
+ JsonToken expToken , String msg0 )
1358
1440
{
1359
1441
String msg = String .format ("Unexpected token (%s), expected %s" ,
1360
1442
p .getCurrentToken (), expToken );
1361
1443
if (msg0 != null ) {
1362
1444
msg = msg + ": " +msg0 ;
1363
1445
}
1364
- return InputMismatchException .from (p , msg );
1446
+ return InputMismatchException .from (p , targetType , msg );
1447
+ }
1448
+
1449
+ public JsonMappingException wrongTokenException (JsonParser p , Class <?> targetType ,
1450
+ JsonToken expToken , String msg0 )
1451
+ {
1452
+ String msg = String .format ("Unexpected token (%s), expected %s" ,
1453
+ p .getCurrentToken (), expToken );
1454
+ if (msg0 != null ) {
1455
+ msg = msg + ": " +msg0 ;
1456
+ }
1457
+ return InputMismatchException .from (p , targetType , msg );
1458
+ }
1459
+
1460
+ @ Deprecated // since 2.9
1461
+ public JsonMappingException wrongTokenException (JsonParser p , JsonToken expToken ,
1462
+ String msg )
1463
+ {
1464
+ return wrongTokenException (p , (JavaType ) null , expToken , msg );
1365
1465
}
1366
1466
1367
1467
/**
@@ -1489,7 +1589,7 @@ public JsonMappingException unknownTypeException(JavaType type, String id,
1489
1589
if (extraDesc != null ) {
1490
1590
msg = msg + ": " +extraDesc ;
1491
1591
}
1492
- return JsonMappingException .from (_parser , msg );
1592
+ return InputMismatchException .from (_parser , type , msg );
1493
1593
}
1494
1594
1495
1595
/**
@@ -1500,8 +1600,8 @@ public JsonMappingException unknownTypeException(JavaType type, String id,
1500
1600
*/
1501
1601
@ Deprecated
1502
1602
public JsonMappingException endOfInputException (Class <?> instClass ) {
1503
- return JsonMappingException .from (_parser , "Unexpected end-of-input when trying to deserialize a "
1504
- +instClass .getName ());
1603
+ return InputMismatchException .from (_parser , instClass ,
1604
+ "Unexpected end-of-input when trying to deserialize a " +instClass .getName ());
1505
1605
}
1506
1606
1507
1607
/*
@@ -1519,7 +1619,7 @@ public JsonMappingException endOfInputException(Class<?> instClass) {
1519
1619
* @since 2.8
1520
1620
*
1521
1621
* @deprecate Since 2.9: use a more specific method, or {@link #reportBadDefinition(JavaType, String)},
1522
- * or {@link #reportInputMismatch(String, Object...) } instead
1622
+ * or {@link #reportInputMismatch} instead
1523
1623
*/
1524
1624
@ Deprecated // since 2.9
1525
1625
public void reportMappingException (String msg , Object ... msgArgs )
0 commit comments