Created
January 24, 2013 22:26
-
-
Save kldavis4/4628732 to your computer and use it in GitHub Desktop.
Test case to demonstrate issue with @JsonIdentityInfo and polymorphic classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.fasterxml.jackson.annotation.JsonIdentityInfo; | |
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
| import com.fasterxml.jackson.annotation.ObjectIdGenerators; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class JacksonDeserializationTest2 { | |
| @org.junit.Test | |
| public void test() throws Exception { | |
| List<Bar> bars = new ArrayList<Bar>(); | |
| Bar bar1 = new Bar(); | |
| Foo p1 = new Foo(); | |
| p1.setObject(bar1); | |
| bar1.setFoo(p1); | |
| bars.add(bar1); | |
| RootEntity rootEntity = new RootEntity(); | |
| rootEntity.setBars(bars); | |
| ObjectMapper mapper = new ObjectMapper(); | |
| RootEntity deserialized = mapper.readValue(mapper.writeValueAsString(rootEntity),RootEntity.class); | |
| } | |
| @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class") | |
| public static class BaseEntity { | |
| } | |
| @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") | |
| public static class RootEntity extends BaseEntity | |
| { | |
| private List<Bar> bars; | |
| public List<Bar> getBars() { | |
| return bars; | |
| } | |
| public void setBars(List<Bar> bars) { | |
| this.bars = bars; | |
| } | |
| } | |
| public static class Foo extends BaseEntity { | |
| private BaseEntity object; | |
| public BaseEntity getObject() { | |
| return object; | |
| } | |
| public void setObject(BaseEntity object) { | |
| this.object = object; | |
| } | |
| } | |
| @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") | |
| public static class Bar extends BaseEntity | |
| { | |
| private Foo foo; | |
| public Foo getFoo() { | |
| return foo; | |
| } | |
| public void setFoo(Foo foo) { | |
| this.foo = foo; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment