tags:

views:

33

answers:

1
+1  Q: 

json: references

Hi, I'm using JSON with Gson package for java. I have an object where there are some inner references from object in one field to object in another field. For example:

{ "loci": [ { "id": "loc1",
"length": 10000, "start": 2 },
{ "id": "loc2", "length": 100,
"start": 50000 } ], "scenarios": [ { "id": "scen1", "loci": [ { "id": "loc1", "length": 10000, "start": 2 }, { "id": "loc2", "length": 100, "start": 50000 } ] }, ] }

So I actually have only two locus objects defined in my java program (loc1 and loc2), but in the printout the objects are duplicated and appear both under "loci" and inside "scen1". Can JSON reflect that there are only two objects actually (so when I load it, it won't create four loci instead of two)?

+1  A: 

No. JSON deals only in values, not references. You will need to use some sort of convention to indicate that a value is actually a reference, and write a custom serializer/deserializer.

Ignacio Vazquez-Abrams
thanks, I post another follow up
David B
See here: http://stackoverflow.com/questions/3362939/passing-a-data-structure-from-java-to-perl-and-vice-versa
David B