diff --git a/lib/index.js b/lib/index.js index 87555bc..c32b85b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,17 @@ var cycle = require('./cycle'); exports.stringify = function stringify(value, replacer, space, _options) { + + if (arguments.length < 4) { + try { + if (arguments.length === 1) { + return JSON.stringify(value); + } else { + return JSON.stringify.apply(JSON, arguments); + } + } catch (e) {} + } + var options = _options || false; if (typeof options === 'boolean') { options = { @@ -28,12 +39,14 @@ exports.stringify = function stringify(value, replacer, space, _options) { exports.parse = function parse(text, reviver) { var needsRetrocycle = /"\$jsan"/.test(text); - var replaced = JSON.parse(text); - var resolved = cycle.retrocycle(replaced); - if(reviver) { - var temp = JSON.stringify(resolved); - var parsed = JSON.parse(temp, reviver); - return parsed; + var parsed; + if (arguments.length === 1) { + parsed = JSON.parse(text); + } else { + parsed = JSON.parse(text, reviver); + } + if (needsRetrocycle) { + parsed = cycle.retrocycle(parsed); } - return resolved; + return parsed; } diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 2a7d779..0000000 --- a/package-lock.json +++ /dev/null @@ -1,322 +0,0 @@ -{ - "name": "jsan", - "version": "3.1.10", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "balanced-match": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true - }, - "benchmark": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz", - "integrity": "sha1-CfPeMckWQl1JjMLuVloOvzwqVik=", - "dev": true, - "requires": { - "lodash": "4.17.10", - "platform": "1.3.5" - } - }, - "brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, - "requires": { - "balanced-match": "1.0.0", - "concat-map": "0.0.1" - } - }, - "circular-json": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz", - "integrity": "sha512-UZK3NBx2Mca+b5LsG7bY183pHWt5Y1xts4P3Pz7ENTwGVnJOUWbRb3ocjvX7hx9tq/yTAdclXm9sZ38gNuem4A==", - "dev": true - }, - "concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true - }, - "fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true - }, - "glob": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", - "dev": true, - "requires": { - "fs.realpath": "1.0.0", - "inflight": "1.0.6", - "inherits": "2.0.3", - "minimatch": "3.0.4", - "once": "1.4.0", - "path-is-absolute": "1.0.1" - } - }, - "immutable": { - "version": "3.8.2", - "resolved": "https://registry.npmjs.org/immutable/-/immutable-3.8.2.tgz", - "integrity": "sha1-wkOZUUVbs5kT2vKBN28VMOEErfM=", - "dev": true - }, - "inflight": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, - "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" - } - }, - "inherits": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", - "dev": true - }, - "jsan": { - "version": "3.1.10", - "resolved": "https://registry.npmjs.org/jsan/-/jsan-3.1.10.tgz", - "integrity": "sha512-Rpme/mJFG3BlIM8/9L+0qAIGccx6dyYEODdkZUHYKyJI3NIl6d13buXa7aE3lO1kZAGMalG0/6QzalXdDdUc4g==", - "dev": true - }, - "json-stringify-safe": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", - "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=", - "dev": true - }, - "lodash": { - "version": "4.17.10", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", - "dev": true - }, - "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, - "requires": { - "brace-expansion": "1.1.11" - } - }, - "mobx": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/mobx/-/mobx-2.7.0.tgz", - "integrity": "sha1-zz2C0YwMp/RY2PKiQIF7PcflSgE=", - "dev": true - }, - "mocha": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-2.2.1.tgz", - "integrity": "sha1-BKL4rrFJ/lAXfgCn710IxjnpEms=", - "dev": true, - "requires": { - "commander": "2.3.0", - "debug": "2.0.0", - "diff": "1.0.8", - "escape-string-regexp": "1.0.2", - "glob": "3.2.3", - "growl": "1.8.1", - "jade": "0.26.3", - "mkdirp": "0.5.0", - "supports-color": "1.2.1" - }, - "dependencies": { - "commander": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz", - "integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=", - "dev": true - }, - "debug": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.0.0.tgz", - "integrity": "sha1-ib2d9nMrUSVrxnBTQrugLtEhMe8=", - "dev": true, - "requires": { - "ms": "0.6.2" - }, - "dependencies": { - "ms": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-0.6.2.tgz", - "integrity": "sha1-2JwhJMb9wTU9Zai3e/GqxLGTcIw=", - "dev": true - } - } - }, - "diff": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/diff/-/diff-1.0.8.tgz", - "integrity": "sha1-NDJ2MI7Jkbe8giZ+1VvBQR+XFmY=", - "dev": true - }, - "escape-string-regexp": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz", - "integrity": "sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=", - "dev": true - }, - "glob": { - "version": "3.2.3", - "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.3.tgz", - "integrity": "sha1-4xPusknHr/qlxHUoaw4RW1mDlGc=", - "dev": true, - "requires": { - "graceful-fs": "2.0.3", - "inherits": "2.0.1", - "minimatch": "0.2.14" - }, - "dependencies": { - "graceful-fs": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-2.0.3.tgz", - "integrity": "sha1-fNLNsiiko/Nule+mzBQt59GhNtA=", - "dev": true - }, - "inherits": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz", - "integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=", - "dev": true - }, - "minimatch": { - "version": "0.2.14", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz", - "integrity": "sha1-x054BXT2PG+aCQ6Q775u9TpqdWo=", - "dev": true, - "requires": { - "lru-cache": "2.5.0", - "sigmund": "1.0.0" - }, - "dependencies": { - "lru-cache": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz", - "integrity": "sha1-2COIrpyWC+y+oMc7uet5tsbOmus=", - "dev": true - }, - "sigmund": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz", - "integrity": "sha1-ZqKzp0mui1+4nv1PzAHclPvgIpY=", - "dev": true - } - } - } - } - }, - "growl": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/growl/-/growl-1.8.1.tgz", - "integrity": "sha1-Sy3sjZB+k9szZiTc7AGDUC+MlCg=", - "dev": true - }, - "jade": { - "version": "0.26.3", - "resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz", - "integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=", - "dev": true, - "requires": { - "commander": "0.6.1", - "mkdirp": "0.3.0" - }, - "dependencies": { - "commander": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz", - "integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=", - "dev": true - }, - "mkdirp": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz", - "integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=", - "dev": true - } - } - }, - "mkdirp": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.0.tgz", - "integrity": "sha1-HXMHam35hs2TROFecfzAWkyavxI=", - "dev": true, - "requires": { - "minimist": "0.0.8" - }, - "dependencies": { - "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true - } - } - }, - "supports-color": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.1.tgz", - "integrity": "sha1-Eu4hUHCGzZjBBY2ewPSsR2t687I=", - "dev": true - } - } - }, - "once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, - "requires": { - "wrappy": "1.0.2" - } - }, - "path-is-absolute": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true - }, - "platform": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.5.tgz", - "integrity": "sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==", - "dev": true - }, - "remotedev-serialize": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/remotedev-serialize/-/remotedev-serialize-0.1.1.tgz", - "integrity": "sha1-D1mAALfddRXWf5tRph0hHhjOlVQ=", - "dev": true, - "requires": { - "jsan": "3.1.10" - } - }, - "rimraf": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz", - "integrity": "sha512-lreewLK/BlghmxtfH36YYVg1i8IAce4TI7oao75I1g245+6BctqTVQiBP3YUJ9C6DQOXJmkYR9X9fCLtCOJc5w==", - "dev": true, - "requires": { - "glob": "7.1.2" - } - }, - "wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true - } - } -} diff --git a/package.json b/package.json index 47e9373..58a3c49 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,11 @@ "devDependencies": { "benchmark": "^2.1.2", "circular-json": "^0.3.0", - "immutable": "^3.8.2", + "immutable": "^3.7.6", "json-stringify-safe": "^5.0.1", "mobx": "^2.4.1", "mocha": "^2.2.1", - "rimraf": "^2.5.2", - "remotedev-serialize": "^0.1.1" + "rimraf": "^2.5.2" }, "dependencies": {}, "repository": { diff --git a/test/regression-remotedev-serialize.js b/test/regression-remotedev-serialize.js deleted file mode 100644 index 5793f42..0000000 --- a/test/regression-remotedev-serialize.js +++ /dev/null @@ -1,32 +0,0 @@ -var assert = require('assert'); -var immutable = require('immutable'); -var serialize = require('remotedev-serialize/immutable/serialize'); - -var jsan = require('../'); - -describe('remotedev-serialize', function() { - it('tests immutable js serialization/deserialization with references', function() { - var sharedValue = []; - var record = immutable.Record({ - prop: sharedValue, - }); - - var refs = [record]; - - var obj = immutable.Map({ - fst: new record(), - scnd: new record(), - }); - var serialized = jsan.stringify(obj, serialize(immutable, refs).replacer, null, true); - var parsed = JSON.parse(serialized); - - var fstProp = parsed.data.fst.data.prop; - var scndProp = parsed.data.scnd.data.prop; - - assert.notStrictEqual(fstProp, scndProp); - assert.ok(scndProp.hasOwnProperty('$jsan') && (typeof scndProp.$jsan === 'string' || parsed.data.scnd.data.prop.$jsan instanceof String)); - - var deserialized = jsan.parse(serialized, serialize(immutable, refs).reviver); - assert.ok(Array.isArray(deserialized.get('scnd').get('prop')), 'deserialized structure has unresolved references'); - }); -}); diff --git a/test/unit.js b/test/unit.js index 65e4648..4f2a851 100644 --- a/test/unit.js +++ b/test/unit.js @@ -228,6 +228,13 @@ describe('jsan', function() { var str = '{"a":1,"b":"string","c":[2,3],"d":null}'; assert.deepEqual(JSON.parse(str), jsan.parse(str)); }); + + it('uses reviver function', function() { + var str = '{"a":1,"b":null}'; + assert.deepEqual({"a":1,"b":2}, + jsan.parse(str, function (key, value) { return key === "b" ? 2 : value; }) + ); + }); it('can decode dates', function() { var str = '{"$jsan":"d1400000000000"}'; @@ -235,6 +242,12 @@ describe('jsan', function() { assert(obj instanceof Date); }); + it('can decode dates while using reviver', function() { + var str = '{"$jsan":"d1400000000000"}'; + var obj = jsan.parse(str, function (key, value) { return value; }); + assert(obj instanceof Date); + }); + it('can decode regexes', function() { str = '{"$jsan":"r,test"}'; var obj = jsan.parse(str);