Simplest answer I can give is that you should avoid ever using generic
type values as root value (immediate object you give to writeValue() /
readValue()). Generic types are fine everywhere else reachable through
POJO properties, but at root level Java Type Erasure causes problems.
In this case for example, try to see if something like:
public class Wrapper {
public List<A> value; // or with getter/setter
}
would work better.
There are alternate ways to deal with generic typed root values too
(pass `TypeReference` to create `ObjectWrite`,
then call `writeValue()`; sub-class `List<A>` as `class AList extends
List<A> { }`), but I really think it is best to avoid
this class of problems by using non-generic wrapper type.
-+ Tatu +-
On Tue, Nov 27, 2018 at 8:05 PM <[email protected]> wrote:
>
> 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.
--
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.