gson-fire icon indicating copy to clipboard operation
gson-fire copied to clipboard

Preventing infinite recursion caused by circular references

Open lalpert opened this issue 8 years ago • 6 comments

Jackson has a solution to serializing classes with bidirection references using @JsonIdentityInfo (For example, see http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion#json-identity-info). This way, you can serialize either object, and rather than causing a stack overflow error, it will just include a reference back to the object already included in the serialization. It seems like Gson does not have a solution like that. Could that be added to gson-fire?

lalpert avatar Sep 14 '16 00:09 lalpert

Thanks for your suggestion, I will try to include this or something similar in 1.8.0 which I will be released around mid-oct.

julman99 avatar Oct 02 '16 03:10 julman99

Any progress with this issue? Is there some solution to handle cyclic reference and avoid stack overflow (other than remove one side of bidirectional reference)?

Thanks!

Typografikon avatar Oct 10 '17 23:10 Typografikon

Is there some update about that? I've been looking for a elegant solution to this since ever and nothing found. For while..

interappcci avatar Jul 26 '18 23:07 interappcci

Sorry I've been super busy and lost track of this. I'll think about something based on what @lalpert suggested and will reply back here before the end of the week with a timeline.

julman99 avatar Jul 30 '18 14:07 julman99

Would it work if I create an annotation to override the type adapter of a particular field in a class? For example

public class Person {
    private int id;
    private String name;
    private House house;
}

public class House {
    @TypeAdapter(SimplePersonAdapter.class)
    private Person owner;
}

public class SimplePersonAdapter implements TypeAdapter<Person> {
     //this class just serializes a simplified version of a Person object, for example it could just serialize the id
}

julman99 avatar Jul 30 '18 22:07 julman99

@interappcci, @lalpert I think I will include my suggestion in my previous comment since it solves also a different requirement that was raised to me the other day.

Does that work for both of you in regards to the circular reference problem?

julman99 avatar Oct 23 '18 18:10 julman99

I just need to cxclude a field at the second nested while serializing a object:

class User {
   String name;
   User createdBy;
}

class JsonRespWrap<D> {
   int code;
   User data;
  public JsonRespWrap (D data ) {
    this.data = data
    this.code = 0
  }
}

User user = userDao.findOne(1)

gson.toJson(new JsonRespWrap(user)); //  data.createdBy.createdBy should be excluded but data.createdBy should be included

// here is the exclusion stratety

public class GsonExclusionStrategy implements ExclusionStrategy{
   //...

    @Override
    public boolean shouldSkipClass(Class<?> clazz) {
        return true;
    }

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        //how to do whthout parent FieldAttributes (or a stack of FieldAttributeses) provided as parameter
    }
}

aninfeel avatar Apr 02 '19 07:04 aninfeel