如何解决JSON序列化中的循环引用?

时间:2022-08-23 00:23:40

Help me, please, to store tree structure in JSON format. I got

请帮助我以JSON格式存储树结构。我有

ERROR 6444 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].    [dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.*Error] with root cause

java.lang.*Error: null
    at java.lang.Class.getGenericInterfaces(Class.java:912) ~[na:1.8.0_40]
    at com.fasterxml.jackson.databind.type.TypeFactory._doFindSuperInterfaceChain(TypeFactory.java:1260) ~[jackson-databind-2.6.6.jar:2.6.6]
    at com.fasterxml.jackson.databind.type.TypeFactory._findSuperInterfaceChain(TypeFactory.java:1254) ~[jackson-databind-2.6.6.jar:2.6.6]
    // more stacktrace here...

on the folowing code:

在下面的代码:

public class CustomASTNode implements ASTNode {
    private final CustomNode node; // simple property
    private final ASTNodeID id; // simple property
    @JsonBackReference
    private final ASTNode parent; // circular property!
    @JsonManagedReference
    private List<ASTNode> children = new ArrayList<>(); // circular property!
    // more code
}

public interface ASTNode extends Iterable<ASTNode> { // ?
    // more code
}

I used @JsonBackReference and @JsonManagedReference annotations to deal with fields, but have no ideas how to resolve recursion in interface. Is it possible or maybe it is necessary to rewrite these snippet?

我使用@JsonBackReference和@JsonManagedReference注释来处理字段,但是没有想法如何解决接口中的递归。是否可能或者有必要重写这些片段?

1 个解决方案

#1


1  

Take a look at http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

看看http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

A simple example would be:

一个简单的例子是:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Identifiable
{
    public int value;

    public Identifiable next;
}

and if we created a cycle consisting of two values, like:

如果我们创建一个由两个值组成的循环,例如:

Identifiable ob1 = new Identifiable();
ob1.value = 13;
Identifiable ob2 = new Identifiable();
ob2.value = 42;
// link as a cycle:
ob1.next = ob2;
ob2.next = ob1;

and serialized using:

和序列化使用:

String json = objectMapper.writeValueAsString(ob1);

we would get following serialization for JSON:

我们将获得JSON的序列化:

{
    "@id" : 1,
    "value" : 13,
    "next" : {
        "@id" : 2,
        "value" : 42,
        "next" : 1
    }
}

#1


1  

Take a look at http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

看看http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

A simple example would be:

一个简单的例子是:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Identifiable
{
    public int value;

    public Identifiable next;
}

and if we created a cycle consisting of two values, like:

如果我们创建一个由两个值组成的循环,例如:

Identifiable ob1 = new Identifiable();
ob1.value = 13;
Identifiable ob2 = new Identifiable();
ob2.value = 42;
// link as a cycle:
ob1.next = ob2;
ob2.next = ob1;

and serialized using:

和序列化使用:

String json = objectMapper.writeValueAsString(ob1);

we would get following serialization for JSON:

我们将获得JSON的序列化:

{
    "@id" : 1,
    "value" : 13,
    "next" : {
        "@id" : 2,
        "value" : 42,
        "next" : 1
    }
}