|
1 | 1 | package com.fasterxml.jackson.failing;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.*; |
4 | 6 | import com.fasterxml.jackson.databind.*;
|
| 7 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
5 | 8 |
|
6 | 9 | public class TestTreeWithType extends BaseMapTest
|
7 | 10 | {
|
8 |
| - public static class Foo { |
9 |
| - public String bar; |
10 | 11 |
|
11 |
| - public Foo() { } |
| 12 | + // [Issue#353] |
| 13 | + public class SavedCookie { |
| 14 | + public String name, value; |
12 | 15 |
|
13 |
| - public Foo(String bar) { |
14 |
| - this.bar = bar; |
| 16 | + public SavedCookie() { } |
| 17 | + public SavedCookie(String n, String v) { |
| 18 | + name = n; |
| 19 | + value = v; |
15 | 20 | }
|
16 | 21 | }
|
17 |
| - |
| 22 | + |
| 23 | + public class SavedCookieDeserializer extends JsonDeserializer<SavedCookie> { |
| 24 | + @Override |
| 25 | + public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt) |
| 26 | + throws IOException { |
| 27 | + ObjectCodec oc = jsonParser.getCodec(); |
| 28 | + JsonNode node = oc.readTree(jsonParser); |
| 29 | + return new SavedCookie(node.path("name").textValue(), |
| 30 | + node.path("value").textValue()); |
| 31 | + } |
| 32 | + } |
18 | 33 | /*
|
19 | 34 | /**********************************************************
|
20 | 35 | /* Unit tests
|
21 | 36 | /**********************************************************
|
22 | 37 | */
|
23 | 38 |
|
24 |
| - private final ObjectMapper mapper = new ObjectMapper(); |
25 |
| - |
26 |
| - public void testValueAsStringWithoutDefaultTyping() throws Exception { |
27 |
| - |
28 |
| - Foo foo = new Foo("baz"); |
29 |
| - String json = mapper.writeValueAsString(foo); |
30 |
| - |
31 |
| - JsonNode jsonNode = mapper.readTree(json); |
32 |
| - assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
33 |
| - } |
34 |
| - |
35 |
| - public void testValueAsStringWithDefaultTyping() throws Exception { |
36 |
| - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
37 |
| - |
38 |
| - Foo foo = new Foo("baz"); |
39 |
| - String json = mapper.writeValueAsString(foo); |
40 |
| - |
41 |
| - JsonNode jsonNode = mapper.readTree(json); |
42 |
| - assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
43 |
| - } |
44 |
| - |
45 |
| - public void testReadTreeWithDefaultTyping() throws Exception |
| 39 | + public void testIssue353() throws Exception |
46 | 40 | {
|
47 |
| - final String CLASS = Foo.class.getName(); |
| 41 | + ObjectMapper mapper = new ObjectMapper(); |
48 | 42 |
|
49 |
| - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, |
50 |
| - JsonTypeInfo.As.PROPERTY); |
51 |
| - String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}"; |
52 |
| - JsonNode jsonNode = mapper.readTree(json); |
53 |
| - assertEquals(jsonNode.get("bar").textValue(), "baz"); |
54 |
| - } |
55 |
| - |
56 |
| - public void testValueToTreeWithoutDefaultTyping() throws Exception { |
| 43 | + mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class"); |
57 | 44 |
|
58 |
| - Foo foo = new Foo("baz"); |
59 |
| - JsonNode jsonNode = mapper.valueToTree(foo); |
60 |
| - assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
61 |
| - } |
| 45 | + SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST")); |
| 46 | + testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer()); |
| 47 | + mapper.registerModule(testModule); |
62 | 48 |
|
63 |
| - public void testValueToTreeWithDefaultTyping() throws Exception { |
64 |
| - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
| 49 | + SavedCookie savedCookie = new SavedCookie("key", "v"); |
| 50 | + String json = mapper.writeValueAsString(savedCookie); |
| 51 | + |
| 52 | + SavedCookie out = mapper.reader(SavedCookie.class).readValue(json); |
65 | 53 |
|
66 |
| - Foo foo = new Foo("baz"); |
67 |
| - JsonNode jsonNode = mapper.valueToTree(foo); |
68 |
| - assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
| 54 | + assertEquals("key", out.name); |
| 55 | + assertEquals("v", out.value); |
69 | 56 | }
|
70 | 57 | }
|
0 commit comments