pax_global_header00006660000000000000000000000064121535015510014510gustar00rootroot0000000000000052 comment=c052001632964be46661355d3071da7b3172767c json-stringify-safe-5.0.0/000077500000000000000000000000001215350155100154135ustar00rootroot00000000000000json-stringify-safe-5.0.0/LICENSE000066400000000000000000000024361215350155100164250ustar00rootroot00000000000000Copyright (c) Isaac Z. Schlueter ("Author") All rights reserved. The BSD License Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. json-stringify-safe-5.0.0/README.md000066400000000000000000000021721215350155100166740ustar00rootroot00000000000000# json-stringify-safe Like JSON.stringify, but doesn't throw on circular references. ## Usage Takes the same arguments as `JSON.stringify`. ```javascript var stringify = require('json-stringify-safe'); var circularObj = {}; circularObj.circularRef = circularObj; circularObj.list = [ circularObj, circularObj ]; console.log(stringify(circularObj, null, 2)); ``` Output: ```json { "circularRef": "[Circular]", "list": [ "[Circular]", "[Circular]" ] } ``` ## Details ``` stringify(obj, serializer, indent, decycler) ``` The first three arguments are the same as to JSON.stringify. The last is an argument that's only used when the object has been seen already. The default `decycler` function returns the string `'[Circular]'`. If, for example, you pass in `function(k,v){}` (return nothing) then it will prune cycles. If you pass in `function(k,v){ return {foo: 'bar'}}`, then cyclical objects will always be represented as `{"foo":"bar"}` in the result. ``` stringify.getSerialize(serializer, decycler) ``` Returns a serializer that can be used elsewhere. This is the actual function that's passed to JSON.stringify. json-stringify-safe-5.0.0/package.json000066400000000000000000000007701215350155100177050ustar00rootroot00000000000000{ "name": "json-stringify-safe", "version": "5.0.0", "description": "Like JSON.stringify, but doesn't blow up on circular refs", "main": "stringify.js", "scripts": { "test": "node test.js" }, "repository": { "type": "git", "url": "git://github.com/isaacs/json-stringify-safe" }, "keywords": [ "json", "stringify", "circular", "safe" ], "author": "Isaac Z. Schlueter (http://blog.izs.me)", "license": "BSD", "readmeFilename": "README.md" } json-stringify-safe-5.0.0/stringify.js000066400000000000000000000016721215350155100177750ustar00rootroot00000000000000module.exports = stringify; function getSerialize (fn, decycle) { var seen = [], keys = []; decycle = decycle || function(key, value) { return '[Circular ' + getPath(value, seen, keys) + ']' }; return function(key, value) { var ret = value; if (typeof value === 'object' && value) { if (seen.indexOf(value) !== -1) ret = decycle(key, value); else { seen.push(value); keys.push(key); } } if (fn) ret = fn(key, ret); return ret; } } function getPath (value, seen, keys) { var index = seen.indexOf(value); var path = [ keys[index] ]; for (index--; index >= 0; index--) { if (seen[index][ path[0] ] === value) { value = seen[index]; path.unshift(keys[index]); } } return '~' + path.join('.'); } function stringify(obj, fn, spaces, decycle) { return JSON.stringify(obj, getSerialize(fn, decycle), spaces); } stringify.getSerialize = getSerialize; json-stringify-safe-5.0.0/test.js000066400000000000000000000043431215350155100167340ustar00rootroot00000000000000var stringify = require('./stringify.js'); var circularObj = { a: 'b' }; circularObj.circularRef = circularObj; circularObj.list = [ circularObj, circularObj ]; ////////// // default var testObj = { "a": "b", "circularRef": "[Circular ~]", "list": [ "[Circular ~]", "[Circular ~]" ] }; var assert = require('assert'); assert.equal(JSON.stringify(testObj, null, 2), stringify(circularObj, null, 2)); assert.equal(JSON.stringify(testObj, null, 2), JSON.stringify(circularObj, stringify.getSerialize(), 2)); //////// // prune testObj = { "a": "b", "list": [ null, null ] }; function prune(k, v) {} assert.equal(JSON.stringify(testObj, null, 2), stringify(circularObj, null, 2, prune)); /////////// // re-cycle // (throws) function recycle(k, v) { return v; } assert.throws(function() { stringify(circularObj, null, 2, recycle); }); //////// // fancy testObj = { "a": "b", "circularRef": "circularRef{a:string,circularRef:Object,list:Array}", "list": [ "0{a:string,circularRef:Object,list:Array}", "1{a:string,circularRef:Object,list:Array}" ] }; function signer(key, value) { var ret = key + '{'; var f = false; for (var i in value) { if (f) ret += ','; f = true; ret += i + ':'; var v = value[i]; switch (typeof v) { case 'object': if (!v) ret += 'null'; else if (Array.isArray(v)) ret += 'Array' else ret += v.constructor && v.constructor.name || 'Object'; break; default: ret += typeof v; break; } } ret += '}'; return ret; } assert.equal(JSON.stringify(testObj, null, 2), stringify(circularObj, null, 2, signer)); /////// //multi var a = { x: 1 }; a.a = a; var b = { x: 2 }; b.a = a; var c = { a: a, b: b }; var d = { list: [ a, b, c ] }; d.d = d; var multi = { "list": [ { "x": 1, "a": "[Circular ~.list.0]" }, { "x": 2, "a": "[Circular ~.list.0]" }, { "a": "[Circular ~.list.0]", "b": "[Circular ~.list.1]" } ], "d": "[Circular ~]" }; assert.equal(JSON.stringify(multi, null, 2), stringify(d, null, 2)); //////// // pass! console.log('ok');