@@ -840,10 +840,12 @@ public boolean handleUnknownProperty(JsonParser p, JsonDeserializer<?> deser,
840
840
}
841
841
842
842
/**
843
- * Method that deserializers should call if they encounter an unrecognized
844
- * property (and once that is not explicitly designed as ignorable), to
845
- * inform possibly configured {@link DeserializationProblemHandler}s and
846
- * let it handle the problem.
843
+ * Method that deserializers should call if they encounter a String value
844
+ * that can not be converted to expected key of a {@link java.util.Map}
845
+ * valued property.
846
+ * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue}
847
+ * on configured handlers, if any, to allow for recovery; if recovery does not
848
+ * succeed, will throw {@link InvalidFormatException} with given message.
847
849
*
848
850
* @param keyClass Expected type for key
849
851
* @param keyValue String value from which to deserialize key
@@ -852,7 +854,7 @@ public boolean handleUnknownProperty(JsonParser p, JsonDeserializer<?> deser,
852
854
*
853
855
* @return Key value to use
854
856
*
855
- * @throws IOException
857
+ * @throws IOException To indicate unrecoverable problem, usually based on <code>msg</code>
856
858
*
857
859
* @since 2.8
858
860
*/
@@ -876,6 +878,46 @@ public Object handleWeirdKey(Class<?> keyClass, String keyValue,
876
878
throw weirdKeyException (keyClass , keyValue , msg );
877
879
}
878
880
881
+ /**
882
+ * Method that deserializers should call if they encounter a numeric value
883
+ * that can not be converted to target property type, in cases where some
884
+ * numeric values could be acceptable (either with different settings,
885
+ * or different numeric value).
886
+ * Default implementation will try to call {@link DeserializationProblemHandler#handleWeirdNumberValue}
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 Number 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 handleWeirdNumberValue (Class <?> targetClass , Number 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 ().handleWeirdNumberValue (this , targetClass , value , msg );
913
+ if (key != DeserializationProblemHandler .NOT_HANDLED ) {
914
+ return key ;
915
+ }
916
+ h = h .next ();
917
+ }
918
+ throw weirdNumberException (value , targetClass , msg );
919
+ }
920
+
879
921
/**
880
922
* @since 2.8
881
923
*/
@@ -968,19 +1010,6 @@ public void reportWeirdStringException(String value, Class<?> instClass,
968
1010
throw weirdStringException (value , instClass , msg );
969
1011
}
970
1012
971
- /**
972
- * @since 2.8
973
- */
974
- public void reportWeirdNumberException (Number value , Class <?> instClass ,
975
- String msg , Object ... msgArgs )
976
- throws JsonMappingException
977
- {
978
- if (msgArgs .length > 0 ) {
979
- msg = String .format (msg , msgArgs );
980
- }
981
- throw weirdNumberException (value , instClass , msg );
982
- }
983
-
984
1013
/**
985
1014
* @since 2.8
986
1015
*/
@@ -1081,6 +1110,9 @@ public JsonMappingException mappingException(String msgTemplate, Object... args)
1081
1110
* Helper method for constructing exception to indicate that given JSON
1082
1111
* Object field name was not in format to be able to deserialize specified
1083
1112
* key type.
1113
+ * Note that most of the time this method should NOT be called; instead,
1114
+ * {@link #handleWeirdKey} should be called which will call this method
1115
+ * if necessary, but may also use overrides.
1084
1116
*/
1085
1117
public JsonMappingException weirdKeyException (Class <?> keyClass , String keyValue ,
1086
1118
String msg ) {
@@ -1090,6 +1122,18 @@ public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue
1090
1122
keyValue , keyClass );
1091
1123
}
1092
1124
1125
+ /**
1126
+ * Helper method for constructing exception to indicate that input JSON
1127
+ * Number was not suitable for deserializing into given target type.
1128
+ */
1129
+ public JsonMappingException weirdNumberException (Number value , Class <?> instClass ,
1130
+ String msg ) {
1131
+ return InvalidFormatException .from (_parser ,
1132
+ String .format ("Can not deserialize value of type %s from number %s: %s" ,
1133
+ instClass .getName (), String .valueOf (value ), msg ),
1134
+ value , instClass );
1135
+ }
1136
+
1093
1137
/**
1094
1138
* Helper method for constructing exception to indicate that given JSON
1095
1139
* Object field name was not in format to be able to deserialize specified
@@ -1157,21 +1201,6 @@ public JsonMappingException weirdStringException(String value, Class<?> instClas
1157
1201
value , instClass );
1158
1202
}
1159
1203
1160
- /**
1161
- * Helper method for constructing exception to indicate that input JSON
1162
- * Number was not suitable for deserializing into given target type.
1163
- *
1164
- * @deprecated Since 2.8 use {@link #reportWeirdNumberException} instead
1165
- */
1166
- @ Deprecated
1167
- public JsonMappingException weirdNumberException (Number value , Class <?> instClass ,
1168
- String msg ) {
1169
- return InvalidFormatException .from (_parser ,
1170
- String .format ("Can not deserialize value of type %s from number %s: %s" ,
1171
- instClass .getName (), String .valueOf (value ), msg ),
1172
- value , instClass );
1173
- }
1174
-
1175
1204
/**
1176
1205
* Helper method for indicating that the current token was expected to be another
1177
1206
* token.
0 commit comments