News for hackers. Stuff that matters.
Re:Show HN: JSONSchema to TypeScript compiler by lolive on Wednesday April 26, 2017 @06:08AM (#14201832)
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).
Re:Show HN: JSONSchema to TypeScript compiler by bcherny on Wednesday April 26, 2017 @02:47PM (#14205823)
JSONSchema 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/ma...

- Local $ref: https://github.com/bcherny/json-schema-to-typescript/blob/ma...

- Remote $ref: https://github.com/bcherny/json-schema-to-typescript/blob/ma...

- Cyclical $ref: https://github.com/bcherny/json-schema-to-typescript/blob/ma...

Hope that helps!

[0] https://tools.ietf.org/html/rfc6901

Re:Show HN: JSONSchema to TypeScript compiler by akst on Wednesday April 26, 2017 @07:11AM (#14202075)
You 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.
Re:Show HN: JSONSchema to TypeScript compiler by lolive on Wednesday April 26, 2017 @08:38AM (#14202428)
As 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?
Re:Show HN: JSONSchema to TypeScript compiler by akst on Thursday April 27, 2017 @11:12PM (#14216634)
Not sure what the requirements are for the parent comment, I'm just explaining an approach to serialise a graph data structure.
Re:Show HN: JSONSchema to TypeScript compiler by msoad on Wednesday April 26, 2017 @07:13AM (#14202080)
Look up json pointers and you will be enlightened