Hi, 

I am using jackson for serialization/deserialization
I have no problem with  
- simple objects
- objects with circular reference and no inheritance. 
But when the object implies inheritance and circular reference, the 
deserialization fails.

For example : I have an abstract superclass A ,which has one subclass B. A 
can reference another A.
Here is a test case : 

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, 
property = "id",  scope =  A.class)
@JsonSubTypes( { @Type(value=B.class,name="B")} )
public abstract class A {
    
    private String id;
    
    private A replacement;

    public A() {
        super();
    }
    public A(String id) {
        super();
        this.id  = id;
    }

    public String getId() {
        return id;
    }

    public A getReplacement() {
        return replacement;
    }

    public void setReplacement(A replacement) {
        this.replacement = replacement;
    }
    public String toString() {
        return "Id="+getId()+" " + this.getClass().getSimpleName();
    }
}



@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, 
property = "id",  scope =  B.class)
public class B extends A {

    public B() {
        super();
    }
    public B(String id) {
        super(id);
    }
}


I tests it with 2 objects b1 and b2. 
b1 references b2 and b2 references b1

public static void test() throws IOException {
    // -- Mapper definition
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.USE_EQUALITY_FOR_OBJECT_ID);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);  
    mapper.enableDefaultTyping(DefaultTyping.NON_FINAL,As.PROPERTY);
    
    // -- Test datas
    List<A> aList = new ArrayList<>();
    B b1 = new B("1");
    B b2 = new B("2");
    b1.setReplacement(b2);
    b2.setReplacement(b1);

    aList.add(b1);
    aList.add(b2);
    
    // -- Serialization
    String jsonString;
    jsonString = mapper.writeValueAsString(aList);

    System.out.println(jsonString);

    // -- DeSerialization
    List<Object> list = mapper.readValue(jsonString, List.class);
    System.out.println(list.size()+" elements :");
    for (int i = 0; i< list.size(); i++ ) {
        System.out.println(list.get(i).toString());
    }
 }

which yields to the following error on the readValue: 

[ "java.util.ArrayList", [ {
  "@class" : "B",
  "id" : "1",
  "replacement" : {
    "@class" : "B",
    "id" : "2",
    "replacement" : "1"
  }
}, "2" ] ]
Exception in thread "main" com.fasterxml.jackson.databind.deser.
UnresolvedForwardReference: Unresolved forward references for: Object id [1] 
(for A) at [Source: [ "java.util.ArrayList", [ {
  "@class" : "B",
  "id" : "1",
  "replacement" : {
    "@class" : "B",
    "id" : "2",
    "replacement" : "1"
  }
}, "2" ] ]; line: 7, column: 24].
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.
checkUnresolvedObjectId(DefaultDeserializationContext.java:154)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(
ObjectMapper.java:3738)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.
java:2726)
    at Test.test(Test.java:45)
    at Test.main(Test.java:17)


What am i missing ? 



-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to