google / google/gson

Circular references error

Open
#1,447 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

I'm using 2.8.5 version and I have a StructId class:

`public class StructId implements Serializable {
private static final long serialVersionUID = 1L;

public String Name;
public StructType Type;
public StructId ParentId;
public StructId ChildId;]`

I tried to solve it this way:

`private Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {

public boolean shouldSkipClass(Class clazz) {
return false; //(clazz == StructId.class);
}

/**
* Custom field exclusion goes here
*/
public boolean shouldSkipField(FieldAttributes f) {
//Ignore inner StructIds to solve circular serialization
return ( f.getName().equals("ParentId") || f.getName().equals("ChildId") );
}

})
/**
* Use serializeNulls method if you want To serialize null values
* By default, Gson does not serialize null values
*/
.serializeNulls()
.create();`

But it's not good enough, cause the ParentId/ChildId are critical and can't simply ignore them.

I saw in some posts a suggestion to enable depth of recursion... is it implemented? is there other way to come over this?

Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.