Open GoogleCodeExporter opened 9 years ago
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
Original comment by inder123
on 29 Sep 2009 at 5:54
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
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
Original comment by inder123
on 1 Nov 2010 at 10:33
Circular references should be dealt with via a configurable behavior.
Original comment by zacharys...@gmail.com
on 18 Jan 2011 at 6:07
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
Original comment by limpbizkit
on 30 Dec 2011 at 6:31
Issue 193 has been merged into this issue.
Original comment by limpbizkit
on 30 Dec 2011 at 6:32
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
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
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
Is this issue fixed?
Original comment by vivek.sr...@gmail.com
on 16 Oct 2013 at 7:21
So five years later and ..... ???
Original comment by arron.fe...@gmail.com
on 1 Mar 2014 at 12:34
+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
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
+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
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
Original issue reported on code.google.com by
dave7362...@gmail.com
on 17 Jul 2009 at 4:14