@@ -878,6 +878,46 @@ public Object handleWeirdKey(Class<?> keyClass, String keyValue,
878
878
throw weirdKeyException (keyClass , keyValue , msg );
879
879
}
880
880
881
+ /**
882
+ * Method that deserializers should call if they encounter a String value
883
+ * that can not be converted to target property type, in cases where some
884
+ * String values could be acceptable (either with different settings,
885
+ * or different value).
886
+ * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdStringValue}
887
+ * on configured handlers, if any, to allow for recovery; if recovery does not
888
+ * succeed, will throw {@link InvalidFormatException} with given message.
889
+ *
890
+ * @param targetClass Type of property into which incoming number should be converted
891
+ * @param value String value from which to deserialize property value
892
+ * @param msg Error message template caller wants to use if exception is to be thrown
893
+ * @param msgArgs Optional arguments to use for message, if any
894
+ *
895
+ * @return Property value to use
896
+ *
897
+ * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code>
898
+ *
899
+ * @since 2.8
900
+ */
901
+ public Object handleWeirdStringValue (Class <?> targetClass , String value ,
902
+ String msg , Object ... msgArgs )
903
+ throws IOException
904
+ {
905
+ // but if not handled, just throw exception
906
+ if (msgArgs .length > 0 ) {
907
+ msg = String .format (msg , msgArgs );
908
+ }
909
+ LinkedNode <DeserializationProblemHandler > h = _config .getProblemHandlers ();
910
+ while (h != null ) {
911
+ // Can bail out if it's handled
912
+ Object key = h .value ().handleWeirdStringValue (this , targetClass , value , msg );
913
+ if (key != DeserializationProblemHandler .NOT_HANDLED ) {
914
+ return key ;
915
+ }
916
+ h = h .next ();
917
+ }
918
+ throw weirdStringException (value , targetClass , msg );
919
+ }
920
+
881
921
/**
882
922
* Method that deserializers should call if they encounter a numeric value
883
923
* that can not be converted to target property type, in cases where some
@@ -917,7 +957,7 @@ public Object handleWeirdNumberValue(Class<?> targetClass, Number value,
917
957
}
918
958
throw weirdNumberException (value , targetClass , msg );
919
959
}
920
-
960
+
921
961
/**
922
962
* @since 2.8
923
963
*/
@@ -997,19 +1037,6 @@ public void reportInstantiationException(Class<?> instClass,
997
1037
throw instantiationException (instClass , msg );
998
1038
}
999
1039
1000
- /**
1001
- * @since 2.8
1002
- */
1003
- public void reportWeirdStringException (String value , Class <?> instClass ,
1004
- String msg , Object ... msgArgs )
1005
- throws JsonMappingException
1006
- {
1007
- if (msgArgs .length > 0 ) {
1008
- msg = String .format (msg , msgArgs );
1009
- }
1010
- throw weirdStringException (value , instClass , msg );
1011
- }
1012
-
1013
1040
/**
1014
1041
* @since 2.8
1015
1042
*/
@@ -1112,7 +1139,7 @@ public JsonMappingException mappingException(String msgTemplate, Object... args)
1112
1139
* key type.
1113
1140
* Note that most of the time this method should NOT be called; instead,
1114
1141
* {@link #handleWeirdKey} should be called which will call this method
1115
- * if necessary, but may also use overrides .
1142
+ * if necessary.
1116
1143
*/
1117
1144
public JsonMappingException weirdKeyException (Class <?> keyClass , String keyValue ,
1118
1145
String msg ) {
@@ -1122,9 +1149,33 @@ public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue
1122
1149
keyValue , keyClass );
1123
1150
}
1124
1151
1152
+ /**
1153
+ * Helper method for constructing exception to indicate that input JSON
1154
+ * String was not suitable for deserializing into given target type.
1155
+ * Note that most of the time this method should NOT be called; instead,
1156
+ * {@link #handleWeirdStringValue} should be called which will call this method
1157
+ * if necessary.
1158
+ *
1159
+ * @param value String value from input being deserialized
1160
+ * @param instClass Type that String should be deserialized into
1161
+ * @param msg Message that describes specific problem
1162
+ *
1163
+ * @since 2.1
1164
+ */
1165
+ public JsonMappingException weirdStringException (String value , Class <?> instClass ,
1166
+ String msg ) {
1167
+ return InvalidFormatException .from (_parser ,
1168
+ String .format ("Can not deserialize value of type %s from String %s: %s" ,
1169
+ instClass .getName (), _quotedString (value ), msg ),
1170
+ value , instClass );
1171
+ }
1172
+
1125
1173
/**
1126
1174
* Helper method for constructing exception to indicate that input JSON
1127
1175
* Number was not suitable for deserializing into given target type.
1176
+ * Note that most of the time this method should NOT be called; instead,
1177
+ * {@link #handleWeirdNumberValue} should be called which will call this method
1178
+ * if necessary.
1128
1179
*/
1129
1180
public JsonMappingException weirdNumberException (Number value , Class <?> instClass ,
1130
1181
String msg ) {
@@ -1135,9 +1186,12 @@ public JsonMappingException weirdNumberException(Number value, Class<?> instClas
1135
1186
}
1136
1187
1137
1188
/**
1138
- * Helper method for constructing exception to indicate that given JSON
1139
- * Object field name was not in format to be able to deserialize specified
1140
- * key type.
1189
+ * Helper method for constructing exception to indicate that given type id
1190
+ * could not be resolved to a valid subtype of specified base type, during
1191
+ * polymorphic deserialization.
1192
+ * Note that most of the time this method should NOT be called; instead,
1193
+ * {@link #handleUnknownTypeId} should be called which will call this method
1194
+ * if necessary.
1141
1195
*/
1142
1196
public JsonMappingException unknownTypeIdException (JavaType baseType , String typeId ,
1143
1197
String extraDesc ) {
@@ -1180,27 +1234,6 @@ public JsonMappingException instantiationException(Class<?> instClass, String ms
1180
1234
instClass .getName (), msg ));
1181
1235
}
1182
1236
1183
- /**
1184
- * Method that will construct an exception suitable for throwing when
1185
- * some String values are acceptable, but the one encountered is not.
1186
- *
1187
- * @param value String value from input being deserialized
1188
- * @param instClass Type that String should be deserialized into
1189
- * @param msg Message that describes specific problem
1190
- *
1191
- * @since 2.1
1192
- *
1193
- * @deprecated Since 2.8 use {@link #reportWeirdStringException} instead
1194
- */
1195
- @ Deprecated
1196
- public JsonMappingException weirdStringException (String value , Class <?> instClass ,
1197
- String msg ) {
1198
- return InvalidFormatException .from (_parser ,
1199
- String .format ("Can not deserialize value of type %s from String %s: %s" ,
1200
- instClass .getName (), _quotedString (value ), msg ),
1201
- value , instClass );
1202
- }
1203
-
1204
1237
/**
1205
1238
* Helper method for indicating that the current token was expected to be another
1206
1239
* token.
0 commit comments