| package com.fasterxml.jackson.failing; |
| |
| import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| import com.fasterxml.jackson.databind.*; |
| |
| public class TestTreeWithType extends BaseMapTest |
| { |
| public static class Foo { |
| public String bar; |
| |
| public Foo() { } |
| |
| public Foo(String bar) { |
| this.bar = bar; |
| } |
| } |
| |
| /* |
| /********************************************************** |
| /* Unit tests |
| /********************************************************** |
| */ |
| |
| private final ObjectMapper mapper = new ObjectMapper(); |
| |
| public void testValueAsStringWithoutDefaultTyping() throws Exception { |
| |
| Foo foo = new Foo("baz"); |
| String json = mapper.writeValueAsString(foo); |
| |
| JsonNode jsonNode = mapper.readTree(json); |
| assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
| } |
| |
| public void testValueAsStringWithDefaultTyping() throws Exception { |
| mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
| |
| Foo foo = new Foo("baz"); |
| String json = mapper.writeValueAsString(foo); |
| |
| JsonNode jsonNode = mapper.readTree(json); |
| assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
| } |
| |
| public void testReadTreeWithDefaultTyping() throws Exception |
| { |
| final String CLASS = Foo.class.getName(); |
| |
| mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, |
| JsonTypeInfo.As.PROPERTY); |
| String json = "{\"@class\":\""+CLASS+"\",\"bar\":\"baz\"}"; |
| JsonNode jsonNode = mapper.readTree(json); |
| assertEquals(jsonNode.get("bar").textValue(), "baz"); |
| } |
| |
| public void testValueToTreeWithoutDefaultTyping() throws Exception { |
| |
| Foo foo = new Foo("baz"); |
| JsonNode jsonNode = mapper.valueToTree(foo); |
| assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
| } |
| |
| public void testValueToTreeWithDefaultTyping() throws Exception { |
| mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
| |
| Foo foo = new Foo("baz"); |
| JsonNode jsonNode = mapper.valueToTree(foo); |
| assertEquals(jsonNode.get("bar").textValue(), foo.bar); |
| } |
| } |