How to use @JsonIdentityInfo with circular references?

Burkhard :

I am trying to use the @JsonIdentityInfo from Jackson 2 as described here.

For testing purposes I created the following two classes:

public class A
{
    private B b;
    // constructor(s) and getter/setter omitted
}
public class B
{
    private A a;
    // see above
}

Of course, the naive approach failes:

@Test
public void testJacksonJr() throws Exception
{
    A a = new A();
    B b = new B(a);
    a.setB(b);
    String s = JSON.std.asString(a);// throws StackOverflowError
    Assert.assertEquals("{\"@id\":1,\"b\":{\"@id\":2,\"a\":1}}", s);
}

Adding @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") to class A and/or class B does not work either.

I was hoping that I could serialize (and later deserialize) a to something like this: (not too sure about the JSON though)

{
    "b": {
        "@id": 1,
        "a": {
            "@id": 2,
            "b": 1
        }
    }
}

How can I do that?

Sotirios Delimanolis :

It seems jackson-jr has a subset of Jackson's features. @JsonIdentityInfo must not have made the cut.

If you can use the full Jackson library, just use a standard ObjectMapper with the @JsonIdentityInfo annotation you suggested in your question and serialize your object. For example

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class A {/* all that good stuff */}

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class B {/* all that good stuff */}

and then

A a = new A();
B b = new B(a);
a.setB(b);
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(a));

will generate

{
    "@id": 1,
    "b": {
        "@id": 2,
        "a": 1
    }
}

where the nested a is referring to the root object by its @id.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to detect circular references in JavaScript

How To Use Jackson's @JsonIdentityInfo for Deserialization of directed Graphs?

How to handle class object with circular references?

How do double closures break circular references?

How to use references in Java?

Why use of a WeakSet in detecting circular references make sense?

How can I use @JsonView / @JsonBackReference / @JsonIdentityInfo to serialize desired child elements depending on scenario

How can i avoid circular references in type aliases

How does Python's Garbage Collector Detect Circular References?

How does Python's Garbage Collector Detect Circular References?

GSON: how to prevent StackOverflowError while keeping circular references?

How to deal with circular references in JPA and Hibernate with associative entity

How does Java Garbage Collection work with Circular References?

Value-equals and circular references: how to resolve infinite recursion?

How to avoid circular dependency while adding references to windows runtimecomponent?

How do Typescript templates work with circular references and inheritance?

How do I remove all circular references from an object in JavaScript?

How to use circular progressbar in UITableViewCell?

Persisting circular references in Hibernate

Circular References in Java

Reason for circular references with classes?

Are "circular references" in JPA an antipattern?

Creating circular generic references

Circular references with vals in Kotlin

Circular model references

When and how to use a vector of references

How to replace JsonIdentityInfo's id with the full object?

Using circular FK references in a database

Does Elm allow circular references?