diff --git a/release-notes/CREDITS b/release-notes/CREDITS index cb72ea58f6..4da5f7244f 100644 --- a/release-notes/CREDITS +++ b/release-notes/CREDITS @@ -111,4 +111,8 @@ Will Palmeri: (wpalmeri@github) * Contributed #407: Make array and Collection serializers use configured value null handler (2.4.0) +Cemalettin Koc: (cemo@github) + + * Reported #353: Problems with polymorphic types, `JsonNode` (related to #88) + (2.4.0) diff --git a/release-notes/VERSION b/release-notes/VERSION index e86848c783..3fc975cb17 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -3,6 +3,8 @@ Version: 2.4.0 (xx-xxx-2014) #88: Prevent use of type information for `JsonNode` via default typing (reported by electricmonk@github) +#353: Problems with polymorphic types, `JsonNode` (related to #88) + (reported by cemo@github) #381: Allow inlining/unwrapping of value from single-component JSON array (contributed by yinzara@github) #407: Properly use null handlers for value types when serializer Collection diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java index c6a60369a1..d02ff8c825 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; +import com.fasterxml.jackson.databind.module.SimpleModule; public class TestTreeWithType extends BaseMapTest { @@ -18,6 +20,36 @@ public Foo(String bar) { } } + // [Issue#353] + public class SavedCookie { + public String name, value; + + public SavedCookie() { } + public SavedCookie(String n, String v) { + name = n; + value = v; + } + } + + public class SavedCookieDeserializer extends JsonDeserializer { + @Override + public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt) + throws IOException { + ObjectCodec oc = jsonParser.getCodec(); + JsonNode node = oc.readTree(jsonParser); + return new SavedCookie(node.path("name").textValue(), + node.path("value").textValue()); + } + + @Override + public SavedCookie deserializeWithType(JsonParser jp, DeserializationContext ctxt, + TypeDeserializer typeDeserializer) + throws IOException, JsonProcessingException + { + return (SavedCookie) typeDeserializer.deserializeTypedFromObject(jp, ctxt); + } + } + /* /********************************************************** /* Unit tests @@ -73,4 +105,22 @@ public void testValueToTreeWithDefaultTyping() throws Exception { JsonNode jsonNode = mapper.valueToTree(foo); assertEquals(jsonNode.get("bar").textValue(), foo.bar); } + + public void testIssue353() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + + mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class"); + + SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST")); + testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer()); + mapper.registerModule(testModule); + + SavedCookie savedCookie = new SavedCookie("key", "v"); + String json = mapper.writeValueAsString(savedCookie); + SavedCookie out = mapper.reader(SavedCookie.class).readValue(json); + + assertEquals("key", out.name); + assertEquals("v", out.value); + } } diff --git a/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java b/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java deleted file mode 100644 index 5b91470a1a..0000000000 --- a/src/test/java/com/fasterxml/jackson/failing/TestTreeWithType.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.fasterxml.jackson.failing; - -import java.io.IOException; - -import com.fasterxml.jackson.core.*; -import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.module.SimpleModule; - -public class TestTreeWithType extends BaseMapTest -{ - - // [Issue#353] - public class SavedCookie { - public String name, value; - - public SavedCookie() { } - public SavedCookie(String n, String v) { - name = n; - value = v; - } - } - - public class SavedCookieDeserializer extends JsonDeserializer { - @Override - public SavedCookie deserialize(JsonParser jsonParser, DeserializationContext ctxt) - throws IOException { - ObjectCodec oc = jsonParser.getCodec(); - JsonNode node = oc.readTree(jsonParser); - return new SavedCookie(node.path("name").textValue(), - node.path("value").textValue()); - } - } - /* - /********************************************************** - /* Unit tests - /********************************************************** - */ - - public void testIssue353() throws Exception - { - ObjectMapper mapper = new ObjectMapper(); - - mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class"); - - SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, "TEST", "TEST")); - testModule.addDeserializer(SavedCookie.class, new SavedCookieDeserializer()); - mapper.registerModule(testModule); - - SavedCookie savedCookie = new SavedCookie("key", "v"); - String json = mapper.writeValueAsString(savedCookie); - - SavedCookie out = mapper.reader(SavedCookie.class).readValue(json); - - assertEquals("key", out.name); - assertEquals("v", out.value); - } -}