-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
In some situations the path of a JsonMappingException
doesn't contain the array index where the error occurred.
In the following example we have a custom string deserializer which is disabling the automatic casting from other types (#796).
try {
mapper.readValue("{ \"array\": [ \"foo\", true ]}", Foo.class);
} catch (JsonMappingException exception) {
System.out.println(exception); // 1
for (var node : exception.getPath()) {
System.out.println(node); // 2
System.out.println(node.getFieldName()); // 3
System.out.println(node.getIndex()); // 4
}
}
public class Foo {
public List<String> array;
}
public class StringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if (parser.hasToken(JsonToken.VALUE_STRING)) {
return parser.getText();
} else {
throw context.wrongTokenException(parser, String.class, JsonToken.VALUE_STRING, "");
}
}
}
1: com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (VALUE_TRUE), expected VALUE_STRING:
at [Source: (String)"{ "array": [ "foo", true ]}"; line: 1, column: 21] (through reference chain: Foo["array"])
2: Foo["array"]
3: array
4: -1
In this case, there's no information about the array index in which the error is originating, since the path only has 1 node (it should have two like below).
In other situations we do, for example:
"{ \"array\": [ true, 1 ]}"
public class Foo {
public List<Boolean> array;
}
1: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot coerce Number (1) for type `java.lang.Boolean` (enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow)
at [Source: (PushbackInputStream); line: 2, column: 27] (through reference chain: Foo["array"]->java.util.ArrayList[1])
2: Foo["array"]
3: array
4: -1
2: java.util.ArrayList[1]
3: null
4: 1
Is there something different when using custom deserializers?
Metadata
Metadata
Assignees
Labels
No labels