Json: links

I am using JSON with the Gson package for java. I have an object in which there are some internal links from an object in one field to an object in another field. For instance:

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

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

+3
source share
1 answer

No. JSON only deals with values, not links. You will need to use some kind of convention to indicate that the value is actually a reference and write your own serializer / deserializer.

+3
source

Source: https://habr.com/ru/post/1757140/


All Articles