4 ms·
Just a question. How does JSON and JSONSchema deal with graph structure? For example, what happens when your data structure in memory is A->B->C->A. And you wan
by lolive 10y ago
Just a question.
How does JSON and JSONSchema deal with graph structure?
For example, what happens when your data structure in memory is A->B->C->A. And you want to serialize/deserialize such a data structure.
(as far as i undestand, the JSON serialization of this graph would have duplicated A, once a top level and once inside the serialization of C).
- akst 10y agoYou can probably encode a directed graph by storing the nodes as values in a dictionary which track which nodes they point too, and the keys of the dictionary would be the node identifiers. If you represent it a specific way in your program, just perform a normalisation phase after decoding, and vice versa for encoding.
- lolive 10y agoAs far as I understand, this is the way RDF works. I use N3.js and it basically does that. Is it a need that usually appears when you send JSON between server and client?
- akst 10y agoNot sure what the requirements are for the parent comment, I'm just explaining an approach to serialise a graph data structure.
- msoad 10y agoLook up json pointers and you will be enlightened
- bcherny 10y agoJSONSchema has a concept of "$refs", which is a way to encode a pointer to a value in a JSONSchema. $refs can point to another node in the same graph, a node in another JSONSchema on the same filesystem, or a node in another JSONSchema fetched over the network. $refs are used in JSONSchema, but are described in the separate JSONPointer spec [0]. See unit tests for examples: - In-file $ref: https://github.com/bcherny/json-schema-to-typescript/blob/master/test/e2e/ref.2.ts https://github.com/bcherny/json-schema-to-typescript/blob/ma... - Local $ref: https://github.com/bcherny/json-schema-to-typescript/blob/master/test/e2e/ref.1.ts https://github.com/bcherny/json-schema-to-typescript/blob/ma... - Remote $ref: https://github.com/bcherny/json-schema-to-typescript/blob/master/test/e2e/ref.3.ts https://github.com/bcherny/json-schema-to-typescript/blob/ma... - Cyclical $ref: https://github.com/bcherny/json-schema-to-typescript/blob/master/test/e2e/refWithCycle.1.ts https://github.com/bcherny/json-schema-to-typescript/blob/ma... Hope that helps! [0] https://tools.ietf.org/html/rfc6901 https://tools.ietf.org/html/rfc6901