Skip to content

Commit 8e9d4b2

Browse files
committed
Fix #664
1 parent 07d0e79 commit 8e9d4b2

File tree

7 files changed

+355
-232
lines changed

7 files changed

+355
-232
lines changed

release-notes/VERSION

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Project: jackson-databind
1010
#348: ObjectMapper.valueToTree does not work with @JsonRawValue
1111
(reported by Chris P, pimlottc@github)
1212
#649: Make `BeanDeserializer` use new `parser.nextFieldName()` and `.hasTokenId()` methods
13+
#664: Add `DeserializationFeature.ACCEPT_FLOAT_AS_INT` to prevent coercion of floating point
14+
numbers int `int`/`long`/`Integer`/`Long`
15+
(requested by wenzis@github)
1316
#679: Add `isEmpty()` implementation for `JsonNode` serializers
1417
#688: Provide a means for an ObjectMapper to discover mixin annotation classes on demand
1518
(requested by Laird N)

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

+16-29
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ public Date parseDate(String dateStr) throws IllegalArgumentException
685685
DateFormat df = getDateFormat();
686686
return df.parse(dateStr);
687687
} catch (ParseException e) {
688-
throw new IllegalArgumentException("Failed to parse Date value '"+dateStr+"': "+e.getMessage());
688+
throw new IllegalArgumentException(String.format(
689+
"Failed to parse Date value '%s': %s", dateStr, e.getMessage()));
689690
}
690691
}
691692

@@ -826,7 +827,9 @@ public JsonMappingException mappingException(Class<?> targetClass) {
826827
}
827828

828829
public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
829-
return JsonMappingException.from(_parser, "Can not deserialize instance of "+_calcName(targetClass)+" out of "+token+" token");
830+
return JsonMappingException.from(_parser,
831+
String.format("Can not deserialize instance of %s out of %s token",
832+
_calcName(targetClass), token));
830833
}
831834

832835
/**
@@ -844,23 +847,12 @@ public JsonMappingException mappingException(String message) {
844847
*/
845848
public JsonMappingException instantiationException(Class<?> instClass, Throwable t) {
846849
return JsonMappingException.from(_parser,
847-
"Can not construct instance of "+instClass.getName()+", problem: "+t.getMessage(), t);
850+
String.format("Can not construct instance of %s, problem: %s", instClass.getName(), t.getMessage()), t);
848851
}
849852

850853
public JsonMappingException instantiationException(Class<?> instClass, String msg) {
851-
return JsonMappingException.from(_parser, "Can not construct instance of "+instClass.getName()+", problem: "+msg);
852-
}
853-
854-
/**
855-
* Method that will construct an exception suitable for throwing when
856-
* some String values are acceptable, but the one encountered is not.
857-
*
858-
*
859-
* @deprecated Since 2.1 should use variant that takes value
860-
*/
861-
@Deprecated
862-
public JsonMappingException weirdStringException(Class<?> instClass, String msg) {
863-
return weirdStringException(null, instClass, msg);
854+
return JsonMappingException.from(_parser,
855+
String.format("Can not construct instance of %s, problem: %s", instClass.getName(), msg));
864856
}
865857

866858
/**
@@ -875,26 +867,19 @@ public JsonMappingException weirdStringException(Class<?> instClass, String msg)
875867
*/
876868
public JsonMappingException weirdStringException(String value, Class<?> instClass, String msg) {
877869
return InvalidFormatException.from(_parser,
878-
"Can not construct instance of "+instClass.getName()+" from String value '"+_valueDesc()+"': "+msg,
870+
String.format("Can not construct instance of %s from String value '%s': %s",
871+
instClass.getName(), _valueDesc(), msg),
879872
value, instClass);
880873
}
881874

882-
/**
883-
* Helper method for constructing exception to indicate that input JSON
884-
* Number was not suitable for deserializing into given type.
885-
*/
886-
@Deprecated
887-
public JsonMappingException weirdNumberException(Class<?> instClass, String msg) {
888-
return weirdStringException(null, instClass, msg);
889-
}
890-
891875
/**
892876
* Helper method for constructing exception to indicate that input JSON
893877
* Number was not suitable for deserializing into given target type.
894878
*/
895879
public JsonMappingException weirdNumberException(Number value, Class<?> instClass, String msg) {
896880
return InvalidFormatException.from(_parser,
897-
"Can not construct instance of "+instClass.getName()+" from number value ("+_valueDesc()+"): "+msg,
881+
String.format("Can not construct instance of %s from number value (%s): %s",
882+
instClass.getName(), _valueDesc(), msg),
898883
null, instClass);
899884
}
900885

@@ -905,7 +890,8 @@ public JsonMappingException weirdNumberException(Number value, Class<?> instClas
905890
*/
906891
public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg) {
907892
return InvalidFormatException.from(_parser,
908-
"Can not construct Map key of type "+keyClass.getName()+" from String \""+_desc(keyValue)+"\": "+msg,
893+
String.format("Can not construct Map key of type %s from String \"%s\": ",
894+
keyClass.getName(), _desc(keyValue), msg),
909895
keyValue, keyClass);
910896
}
911897

@@ -914,7 +900,8 @@ public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue
914900
* token.
915901
*/
916902
public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken, String msg0) {
917-
String msg = "Unexpected token ("+p.getCurrentToken()+"), expected "+expToken;
903+
String msg = String.format("Unexpected token (%s), expected %s",
904+
p.getCurrentToken(), expToken);
918905
if (msg0 != null) {
919906
msg = msg + ": "+msg0;
920907
}

src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java

+14
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,20 @@ public enum DeserializationFeature implements ConfigFeature
280280
* @since 2.5
281281
*/
282282
ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT(false),
283+
284+
/**
285+
* Feature that determines whether coercion from JSON floating point
286+
* number (anything with command (`.`) or exponent portion (`e` / `E'))
287+
* to an expected integral number (`int`, `long`, `java.lang.Integer`, `java.lang.Long`,
288+
* `java.math.BigDecimal`) is allowed or not.
289+
* If enabled, coercion truncates value; if disabled, a {@link JsonMappingException}
290+
* will be thrown.
291+
*<p>
292+
* Feature is enabled by default.
293+
*
294+
* @since 2.6
295+
*/
296+
ACCEPT_FLOAT_AS_INT(true),
283297

284298
/**
285299
* Feature that allows unknown Enum values to be parsed as null values.

0 commit comments

Comments
 (0)