Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. 14:22 ~/work/mariusz.cc/% harp compile master*
  2. "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\">&lt;script src=&quot;build/circular-json.js&quot;&gt;&lt;/script&gt;\n</code></pre>\n<pre><code class=\"language-javascript\">&#39;use strict&#39;;\n\nvar CircularJSON = window.CircularJSON\n , obj = { foo: &#39;bar&#39; }\n , str\n ;\n\nobj.self = obj;\nstr = CircularJSON.stringify(obj);\n</code></pre>\n<p>NOTE: Platforms without native JSON (i.e. MSIE &lt;= 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&#39;s the same as native JSON, except the fourth parameter <code>placeholder</code>, which circular references to be replaced with <code>&quot;[Circular]&quot;</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&#39;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 &quot;circularRef&quot;: &quot;[Circular]&quot;,\n &quot;list&quot;: [\n &quot;[Circular]&quot;,\n &quot;[Circular]&quot;\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&#39;t really solving the problem.</p>\n<p>However, if that&#39;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&#39;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&#39;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