I want to represent a DAG as JSON text and wondering if anyone has tried this and any issues they dealt with in regards to validating if the JSON is actually a DAG.
A simple way is to save first all nodes assigning to each of them a node ID, then save all arcs using the node ID of starting and ending node. This will handle all cases (including non-connected graphs, multiply connected graphs, loops... etc.) Show activity on this post.
Specification: DAG-JSON. Status: Descriptive - Final. DAG-JSON supports the full IPLD Data Model. DAG-JSON uses the [JavaScript Object Notation (JSON)] data format, defined by RFC 8259.
JSON Graph is a convention for modeling graph information as a JSON object. Applications that use Falcor represent all their domain data as a single JSON Graph object. JSON Graph is valid JSON and can be parsed by any JSON parser.
Label each node and make an edge list. That is, for each node store the nodes that it has edges to, for example:
{
"a": [ "b", "c", "d" ],
"b": [ "d" ],
"c": [ "d" ],
"d": [ ]
}
You can store many kinds of graphs this way, not just DAGs, so you will need to post-process it to make sure that it has no loops. Just pick a node, DFS, if you see any node more than once it is not a DAG. Then remove all of the nodes you just saw and repeat with any remaining nodes. Do this until you find a loop or you've removed all of the nodes, in the latter case the graph is a DAG.
Note that this does not store parent nodes, since that is redundant information. You can generate those after loading the graph if you need that data.
JSON has no native facility to represent DAGs unless you make your own convention to represent linked data. JSON-LD (a W3C proposal) is a JSON extension that is trying to do exactly that. The proposal can be found here: http://json-ld.org/spec/latest/json-ld/.
Strictly speaking you cannot do it with JSON directly. You'd have to come up with your own way of representing objects that can be identified by reference elsewhere in the data structure, and then you'd have to post-process the result of deserializing the JSON string.
You can't do it with JSON for the simple reason that the JSON expression is the object graph, and there's simply no provisions for expressing the notion that the value of a property should be the value of another property elsewhere in the data structure. To put it another way, no object in the graph can have more than one parent, which implies that every object is the value of exactly one property of one other object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With