Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 14:22 ~/work/mariusz.cc/% harp compile master*
- "Parse Error: <build/circular-json.js></li>\n<li>AMD: <build/circular-json.amd.js></li>\n<li>CommonJS: <build/circular-json.node.js></li>\n</ul>\n<p>(generated via <a href=\"https://github.com/WebReflection/gitstrap\">gitstrap</a>)</p>\n<pre><code class=\"language-html\"><script src="build/circular-json.js"></script>\n</code></pre>\n<pre><code class=\"language-javascript\">'use strict';\n\nvar CircularJSON = window.CircularJSON\n , obj = { foo: 'bar' }\n , str\n ;\n\nobj.self = obj;\nstr = CircularJSON.stringify(obj);\n</code></pre>\n<p>NOTE: Platforms without native JSON (i.e. MSIE <= 8) requires <code>json3.js</code> or similar.</p>\n<p>It is also <em>a bad idea</em> to <code>CircularJSON.parse(JSON.stringify(object))</code> because of those manipulation used in <code>CircularJSON.stringify()</code> able to make parsing safe and secure.</p>\n<p>As summary: <code>CircularJSON.parse(CircularJSON.stringify(object))</code> is the way to go, same is for <code>JSON.parse(JSON.stringify(object))</code>.</p>\n<h1>API</h1><p>It's the same as native JSON, except the fourth parameter <code>placeholder</code>, which circular references to be replaced with <code>"[Circular]"</code> (i.e. for logging).</p>\n<ul>\n<li>CircularJSON.stringify(object, replacer, spacer, placeholder)</li>\n<li>CircularJSON.parse(string, reviver)</li>\n</ul>\n<p>Bear in mind <code>JSON.parse(CircularJSON.stringify(object))</code> will work but not produce the expected output.</p>\n<h1>Similar Libraries</h1><h3>Why Not the <a href=\"https://twitter.com/izs\">@izs</a> One</h3><p>The module <a href=\"https://github.com/isaacs/json-stringify-safe\">json-stringify-safe</a> seems to be for <code>console.log()</code> but it's completely pointless for <code>JSON.parse()</code>, being latter one unable to retrieve back the initial structure. Here an example:</p>\n<pre><code class=\"language-JavaScript\">// a logged object with circular references\n{\n "circularRef": "[Circular]",\n "list": [\n "[Circular]",\n "[Circular]"\n ]\n}\n// what do we do with above output ?\n</code></pre>\n<p>Just type this in your <code>node</code> console: <code>var o = {}; o.a = o; console.log(o);</code>. The output will be <code>{ a: [Circular] }</code> ... good, but that ain't really solving the problem.</p>\n<p>However, if that's all you need, the function used to create that kind of output is probably faster than <code>CircularJSON</code> and surely fits in less lines of code.</p>\n<h3>Why Not {{put random name}} Solution</h3><p>So here the thing: circular references can be wrong but, if there is a need for them, any attempt to ignore them or remove them can be considered just a failure.</p>\n<p>Not because the method is bad or it's not working, simply because the circular info, the one we needed and used in the first place, is lost!</p>\n<p>In this case, <code>CircularJSON</code> does even more than just solve circular and recursions: it maps all same objects so that less memory is used as well on deserialization as less bandwidth too!\nIt's able to redefine those references back later on so the way we store is the way we retrieve and in a reasonably performant way, also trusting the snappy and native <code>JSON</code> methods to iterate.</p>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement