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

Support circular references #137

Open
GoogleCodeExporter opened this issue Mar 19, 2015 · 19 comments
Open

Support circular references #137

GoogleCodeExporter opened this issue Mar 19, 2015 · 19 comments

Comments

@GoogleCodeExporter
Copy link

Objects with Circular References could be supported by choosing a recursion 
depth.  This would 
enable partial serialization of objects and in some cases deserialization.

Original issue reported on code.google.com by dave7362...@gmail.com on 17 Jul 2009 at 4:14

@GoogleCodeExporter
Copy link
Author

I wanted this to be an enhancement request, my apologies for the Defect Listing.

Original comment by dave7362...@gmail.com on 17 Jul 2009 at 4:16

@GoogleCodeExporter
Copy link
Author

Original comment by inder123 on 29 Sep 2009 at 5:54

  • Added labels: Milestone-Release1.5

@GoogleCodeExporter
Copy link
Author

support.
i think gson should provider a strategy to handler Circular Reference.
default strategy should to ignore the field rather than throw exception.

Original comment by joles...@gmail.com on 11 Dec 2009 at 5:57

@GoogleCodeExporter
Copy link
Author

Support.

I fixed the ObjectNavigator class, accept method to catch the 
CircularReferenceException.

The circular references got the same treatment as null fields in my case.
I think a long term solution is support strategies for visiting objects.

One possible strategy is recursion depth, another is ignore and not visit (This 
is my 
approach)

The circular references es very common on entities (very very common if you are 
using 
JPA), so circular references is something gson must deal.

This is a fragment of code used in my case

 Object obj = objTypePair.getObject();
      Object objectToVisit = (obj == null) ? visitor.getTarget() : obj;
      if (objectToVisit == null) {
        return;
      }
      objTypePair.setObject(objectToVisit);
      try {
          visitor.start(objTypePair);
      }
      catch (CircularReferenceException ex){
         return; 
      }

Original comment by utalad...@gmail.com on 24 Feb 2010 at 4:12

@GoogleCodeExporter
Copy link
Author

Original comment by inder123 on 1 Nov 2010 at 10:33

  • Removed labels: Milestone-Release1.5

@GoogleCodeExporter
Copy link
Author

Circular references should be dealt with via a configurable behavior. 

Original comment by zacharys...@gmail.com on 18 Jan 2011 at 6:07

@GoogleCodeExporter
Copy link
Author

I'd prefer to support circular references by serializing such objects as named 
nodes in a graph rather than depth first in a tree.

For example, suppose we have this class structure:
  class Roshambo {
    String name;
    Roshambo beats;
  }
And instance for ROCK, SCISSORS and PAPER. By serializing ROCK with today's 
Gson, it starts writing it out and then fails:
  {
    "name": "ROCK",
    "beats": {
      "name": "SCISSORS",
      "beats": {
        "name": "PAPER",
        "beats": <CircularReferenceException>

Instead we could generate IDs for each instance and refer to values 
symbolically:
  {
    "0x1": {
      "name": "ROCK",
      "beats": "0x2"
    },
    "0x2": {
      "name": "SCISSORS",
      "beats": "0x3"
    },
    "0x3": {
      "name": "PAPER",
      "beats": "0x1"
    }
  }
This is how Java Serialization handles object graphs; it works well. We'd do 
this by creating both a new type and a new type adapter:
  Gson gson = new GsonBuilder()
    .registerTypeAdapterFactory(new SymbolicTypeAdapterFactory())
    .create();
  String json = gson.toJson(new SymbolList(rock));

This could be done as a Gson extension.

Original comment by limpbizkit on 29 Dec 2011 at 5:47

  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

Original comment by limpbizkit on 30 Dec 2011 at 6:31

  • Changed title: Support circular references

@GoogleCodeExporter
Copy link
Author

Issue 193 has been merged into this issue.

Original comment by limpbizkit on 30 Dec 2011 at 6:32

@GoogleCodeExporter
Copy link
Author

One solution:
http://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/co
m/google/gson/graph/GraphAdapterBuilder.java

Original comment by limpbizkit on 18 Mar 2012 at 8:25

@GoogleCodeExporter
Copy link
Author

Why not add the option to serialize multiple instances of the same object as a 
reference to that object, then provide a lightweight Javascript lib to recreate 
the references after a standard JSON.parse() in the browser. This handles a) 
circular references and b) the case where multiple objects share a non-circular 
reference to the same object.

Original comment by rhd...@gmail.com on 26 Apr 2013 at 4:08

@GoogleCodeExporter
Copy link
Author

GraphAdapterBuilder.java as in SVN doesn't work with v2.2.4 anymore, because 
new ConstructorConstructor(); default constructor doesn't seem to exist 
anymore.. Using new ConstructorConstructor(Collections.<Type, 
InstanceCreator<?>>emptyMap()); seems to work.

Original comment by michael....@gmail.com on 30 Sep 2013 at 10:36

@GoogleCodeExporter
Copy link
Author

Is this issue fixed?

Original comment by vivek.sr...@gmail.com on 16 Oct 2013 at 7:21

@GoogleCodeExporter
Copy link
Author

So five years later and ..... ???

Original comment by arron.fe...@gmail.com on 1 Mar 2014 at 12:34

@GoogleCodeExporter
Copy link
Author

+1. A simple but useful feature would be an optional max depth to control 
recursion. Both for when you want to serialise as-best-you-can an entity which 
can have circular references, and for debugging unwanted circular references (a 
limited output would be more informative than a stack-overflow).

Original comment by daniel.winterstein on 30 Mar 2014 at 8:30

@GoogleCodeExporter
Copy link
Author

It would be great of GSON supported the JSOG:

https://github.com/jsog/jsog

There's already a Jackson implementation, although it only handles 
serialization and not deserialization. The javascript, python, and ruby 
implementations are complete.

Original comment by lhori...@gmail.com on 30 Mar 2014 at 11:59

@GoogleCodeExporter
Copy link
Author

+1, this is very much needed. A LOT of use cases in the real world (and I am 
sure at Google) depend on circular references.

Original comment by christop...@gmail.com on 31 Aug 2014 at 9:42

@GoogleCodeExporter
Copy link
Author

Another approach could be to use a JsonPath expression to refer to the parent. 
This will avoid the need to generate ids for objects.

Original comment by inder123 on 7 Jan 2015 at 9:51

@paulo-raca
Copy link

I've implemented JSOG support on #1560 -- It would be great to get some eyes on it

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

No branches or pull requests

3 participants