Skip to content

Commit 5df4c4e

Browse files
committed
Update notes wrt #353
1 parent 9b99cfe commit 5df4c4e

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ Will Palmeri: (wpalmeri@github)
111111
* Contributed #407: Make array and Collection serializers use configured value null handler
112112
(2.4.0)
113113

114+
Cemalettin Koc: (cemo@github)
115+
116+
* Reported #353: Problems with polymorphic types, `JsonNode` (related to #88)
117+
(2.4.0)
114118

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Version: 2.4.0 (xx-xxx-2014)
33

44
#88: Prevent use of type information for `JsonNode` via default typing
55
(reported by electricmonk@github)
6+
#353: Problems with polymorphic types, `JsonNode` (related to #88)
7+
(reported by cemo@github)
68
#381: Allow inlining/unwrapping of value from single-component JSON array
79
(contributed by yinzara@github)
810
#407: Properly use null handlers for value types when serializer Collection

src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java

+50
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.fasterxml.jackson.annotation.JsonTypeInfo;
66
import com.fasterxml.jackson.core.*;
77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
9+
import com.fasterxml.jackson.databind.module.SimpleModule;
810

911
public class TestTreeWithType extends BaseMapTest
1012
{
@@ -18,6 +20,36 @@ public Foo(String bar) {
1820
}
1921
}
2022

23+
// [Issue#353]
24+
public class SavedCookie {
25+
public String name, value;
26+
27+
public SavedCookie() { }
28+
public SavedCookie(String n, String v) {
29+
name = n;
30+
value = v;
31+
}
32+
}
33+
34+
public class SavedCookieDeserializer extends JsonDeserializer<SavedCookie> {
35+
@Override
36+
public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt)
37+
throws IOException {
38+
ObjectCodec oc = jsonParser.getCodec();
39+
JsonNode node = oc.readTree(jsonParser);
40+
return new SavedCookie(node.path("name").textValue(),
41+
node.path("value").textValue());
42+
}
43+
44+
@Override
45+
public SavedCookie deserializeWithType(JsonParser jp, DeserializationContext ctxt,
46+
TypeDeserializer typeDeserializer)
47+
throws IOException, JsonProcessingException
48+
{
49+
return (SavedCookie) typeDeserializer.deserializeTypedFromObject(jp, ctxt);
50+
}
51+
}
52+
2153
/*
2254
/**********************************************************
2355
/* Unit tests
@@ -73,4 +105,22 @@ public void testValueToTreeWithDefaultTyping() throws Exception {
73105
JsonNode jsonNode = mapper.valueToTree(foo);
74106
assertEquals(jsonNode.get("bar").textValue(), foo.bar);
75107
}
108+
109+
public void testIssue353() throws Exception
110+
{
111+
ObjectMapper mapper = new ObjectMapper();
112+
113+
mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class");
114+
115+
SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST"));
116+
testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer());
117+
mapper.registerModule(testModule);
118+
119+
SavedCookie savedCookie = new SavedCookie("key", "v");
120+
String json = mapper.writeValueAsString(savedCookie);
121+
SavedCookie out = mapper.reader(SavedCookie.class).readValue(json);
122+
123+
assertEquals("key", out.name);
124+
assertEquals("v", out.value);
125+
}
76126
}

src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java

-57
This file was deleted.

0 commit comments

Comments
 (0)