{"id":233,"date":"2008-08-21T11:47:16","date_gmt":"2008-08-21T15:47:16","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/?p=233"},"modified":"2008-08-21T11:49:41","modified_gmt":"2008-08-21T15:49:41","slug":"json-serialization-of-interconnected-object-graphs","status":"publish","type":"post","link":"http:\/\/benjamin.smedbergs.us\/blog\/2008-08-21\/json-serialization-of-interconnected-object-graphs\/","title":{"rendered":"JSON serialization of interconnected object graphs"},"content":{"rendered":"<p>In it&#8217;s basic form, JSON cannot serialize cyclic graphs of objects, or graphs where multiple paths can lead to the same object. In a <a href=\"http:\/\/hg.mozilla.org\/users\/bsmedberg_mozilla.com\/dxrpy\/\">project I&#8217;m working on<\/a>, I wanted to move such a graph of highly-interconnected objects from JS to python. So I have invented a format built on top of JSON that can be used to serialize\/deserialize such graphs.<\/p>\n<p>Basically, the JSON comes across as a large list:<\/p>\n<pre>[\r\n  <span style=\"color: #833\">\/* list[0] is the base object at the root of the eventual object graph. *\/<\/span>\r\n  {\r\n    <span style=\"color: #833\">\/* string, number, true\/false, and null properties are serialized directly *\/<\/span>\r\n    <span style=\"color: #383\">\"stringprop\"<\/span>: <span style=\"color: #383\">\"stringvalue\"<\/span>,\r\n    <span style=\"color: #383\">\"numprop\"<\/span>: 3.1415,\r\n    <span style=\"color: #833\">\/* but lists and objects are not serialized directly. Instead, they are represented by an index\r\n       into the base list. \"sharp\" is a nod to <a href=\"http:\/\/developer.mozilla.org\/en\/Sharp_variables_in_JavaScript\">JS sharp variables<\/a>, from which this was originally inspired *\/<\/span>\r\n    <span style=\"color: #383\">\"complexprop\"<\/span>: {<span style=\"color: #383\">\"sharp\"<\/span>: 1}\r\n  },\r\n  <span style=\"color: #833\">\/* list[1] is referenced from list[0].complexprop. It also references itself, see below *\/<\/span>\r\n  [\r\n    <span style=\"color: #383\">\"simplestring\"<\/span>,\r\n    3,\r\n    {<span style=\"color: #383\">\"sharp\"<\/span>: 1}\r\n  ]\r\n]<\/pre>\n<p>You can find JS for serializing these types of graphs <a href=\"http:\/\/hg.mozilla.org\/users\/bsmedberg_mozilla.com\/dxrpy\/index.cgi\/file\/05453eb3a58a\/data\/dxr-collect.js#l17\">here<\/a>, and python for deserializing them <a href=\"http:\/\/hg.mozilla.org\/users\/bsmedberg_mozilla.com\/dxrpy\/index.cgi\/file\/05453eb3a58a\/data\/interjson.py\">here<\/a>.<\/p>\n<p>It turns out that I probably don&#8217;t actually need this code: I&#8217;ve found a simpler solution for my particular problem, but I wanted to share this solution in case other people might find it useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In it&#8217;s basic form, JSON cannot serialize cyclic graphs of objects, or graphs where multiple paths can lead to the same object. In a project I&#8217;m working on, I wanted to move such a graph of highly-interconnected objects from JS to python. So I have invented a format built on top of JSON that can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[39,135,32],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-mozilla","tag-javascript","tag-json","tag-python"],"_links":{"self":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":0,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"wp:attachment":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}