Skip to content

Instantly share code, notes, and snippets.

@kldavis4
Created January 24, 2013 22:26
Show Gist options
  • Select an option

  • Save kldavis4/4628732 to your computer and use it in GitHub Desktop.

Select an option

Save kldavis4/4628732 to your computer and use it in GitHub Desktop.

Revisions

  1. kldavis4 created this gist Jan 24, 2013.
    71 changes: 71 additions & 0 deletions JacksonDeserializationTest2.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    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;
    }
    }
    }