Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing infinite recursion caused by circular references #29

Open
lalpert opened this issue Sep 14, 2016 · 6 comments
Open

Preventing infinite recursion caused by circular references #29

lalpert opened this issue Sep 14, 2016 · 6 comments

Comments

@lalpert
Copy link

lalpert commented Sep 14, 2016

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?

@julman99
Copy link
Owner

julman99 commented Oct 2, 2016

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.

@Typografikon
Copy link

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!

@interappcci
Copy link

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

@julman99
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

@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?

@aninfeel
Copy link

aninfeel commented Apr 2, 2019

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
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants