Skip to content

Commit fdaa293

Browse files
committed
Merge branch '2.x' into 3.x
2 parents f36b2c5 + 5b018f0 commit fdaa293

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tools.jackson.databind.tofix;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import tools.jackson.core.*;
6+
7+
import tools.jackson.databind.*;
8+
import tools.jackson.databind.deser.DeserializationProblemHandler;
9+
import tools.jackson.databind.json.JsonMapper;
10+
import tools.jackson.databind.testutil.DatabindTestUtil;
11+
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
16+
public class DeserializationProblemHandler4656Test extends DatabindTestUtil
17+
{
18+
// For [databind#4656]
19+
static class Person4656 {
20+
public String id;
21+
public String name;
22+
public Long age;
23+
}
24+
25+
static class ProblemHandler4656 extends DeserializationProblemHandler
26+
{
27+
protected static final String NUMBER_LONG_KEY = "$numberLong";
28+
29+
@Override
30+
public Object handleUnexpectedToken(DeserializationContext ctxt, JavaType targetType,
31+
JsonToken t, JsonParser p, String failureMsg)
32+
{
33+
if (targetType.getRawClass().equals(Long.class) && t == JsonToken.START_OBJECT) {
34+
JsonNode tree = p.readValueAsTree();
35+
if (tree.get(NUMBER_LONG_KEY) != null) {
36+
try {
37+
return Long.parseLong(tree.get(NUMBER_LONG_KEY).asString());
38+
} catch (NumberFormatException e) { }
39+
}
40+
}
41+
return NOT_HANDLED;
42+
}
43+
}
44+
45+
// For [databind#4656]
46+
@JacksonTestFailureExpected
47+
@Test
48+
public void testIssue4656() throws Exception {
49+
ObjectMapper mapper = JsonMapper.builder()
50+
.addHandler(new ProblemHandler4656())
51+
.build();
52+
final String json = "{\"id\": \"12ab\", \"name\": \"Bob\", \"age\": {\"$numberLong\": \"10\"}}";
53+
Person4656 person = mapper.readValue(json, Person4656.class);
54+
assertNotNull(person);
55+
assertEquals("12ab", person.id);
56+
assertEquals("Bob", person.name);
57+
assertEquals(10L, person.age);
58+
}
59+
}

0 commit comments

Comments
 (0)