JSPON - Can parse and stringify objects with circular references
npm install jspon| Node.js | 1 Mock Object |
jspon (Id Referencing) x 17,916 ops/sec ±1.49% (87 runs sampled) jspon (JsonPath Referencing) x 22,587 ops/sec ±0.57% (96 runs sampled) Douglas Crockford's cycle#js (JsonPath Referencing) x 15,737 ops/sec ±1.75% (86 runs sampled) npm circular-json x 9,556 ops/sec ±0.90% (87 runs sampled) Fastest is jspon (JsonPath Referencing) |
|---|---|---|
| 20 Mock Objects |
jspon (Id Referencing) x 1,089 ops/sec ±1.27% (91 runs sampled) jspon (JsonPath Referencing) x 1,290 ops/sec ±0.77% (94 runs sampled) Douglas Crockford's cycle#js (JsonPath Referencing) x 933 ops/sec ±1.25% (88 runs sampled) npm circular-json x 475 ops/sec ±1.16% (85 runs sampled) Fastest is jspon (JsonPath Referencing) |
npm i --save jspon
`
Browser
`
Download browser/jspon.js or es_modules/jspon.js from https://github.com/mdavisJr/JSPON-For-JavaScript or use browserify.
`
Setting the JSPON Settings
Only call the setSettings method if you need settings different than the defaults below.
JSPON.setSettings(object); Available settings below.
Field
Data Type
Description
Default Value
idFieldName
string
This option turns on id referencing and allows you to specify the id property name that will be used to track unique objects. The id property name will only show up in the JSON string and will not show up in the object. If this option is set, jsonPathRoot setting will be ignored.
It can not be a property name that exist in your objects.
preserveArrays
boolean
Decides whether or not to preserve references to arrays.
true
jsonPathRoot
string
Allows you to choose what the JSONPath root is.
$
jsonPathFormat
string
Valid values are DotNotation or BracketNotation.
jsonPath Dot-Notation:
$.children[0].name
jsonPath Bracket-Notation:
$['children'][0]['name']
DotNotation
jsonParser
function(str)
Allows you to set your JSON parser of choice
Examples:
JSPON.setSettings({jsonParser: CustomJSON.parse});
--or--
JSPON.setSettings({jsonParser: function(str) {
return JSON.parse(str, function(){
...
});
});
JSON.parse
jsonStringifier
function(obj)
Allows you to set your JSON stringifier of choice
Examples:
JSPON.setSettings({jsonStringifier: CustomJSON.stringify });
--or--
JSPON.setSettings({jsonStringifier: function(obj) {
return JSON.stringify(obj, null, 5);
});
JSON.stringify
Examples (Node.js)
Default Settings jsonPath reference with preserveArrays = true
`
const JSPON = require('jspon');
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"name":"parent","children":[{"name":"John","parent":{"$ref":"$"}},{"name":"Jane","parent":{"$ref":"$"}}],"childrenCopy":{"$ref":"$.children"},"child1":{"$ref":"$.children[0]"},"child2":{"$ref":"$.children[1]"}}
`
jsonPath reference with preserveArrays = false
`
const JSPON = require('jspon');
JSPON.setSettings({ preserveArrays: false });
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"name":"parent","children":[{"name":"John","parent":{"$ref":"$"}},{"name":"Jane","parent":{"$ref":"$"}}],"childrenCopy":[{"$ref":"$.children[0]"},{"$ref":"$.children[1]"}],"child1":{"$ref":"$.children[0]"},"child2":{"$ref":"$.children[1]"}}
`
Id reference with preserveArrays = true
`
const JSPON = require('jspon');
JSPON.setSettings({ idFieldName: '$id' });
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"$id":1,"name":"parent","children":{"$values":[{"$id":3,"name":"John","parent":{"$ref":1}},{"$id":4,"name":"Jane","parent":{"$ref":1}}],"$id":2},"childrenCopy":{"$ref":2},"child1":{"$ref":3},"child2":{"$ref":4}}
`
Id reference with preserveArrays = false
`
const JSPON = require('jspon');
JSPON.setSettings({ idFieldName: '$id', preserveArrays: false });
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"$id":1,"name":"parent","children":[{"$id":2,"name":"John","parent":{"$ref":1}},{"$id":3,"name":"Jane","parent":{"$ref":1}}],"childrenCopy":[{"$ref":2},{"$ref":3}],"child1":{"$ref":2},"child2":{"$ref":3}}
`
jsonPath reference with preserveArrays = true and jsonPathFormat = Bracket-Notation
`
const JSPON = require('jspon');
JSPON.setSettings({ jsonPathFormat: 'BracketNotation' });
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"name":"parent","children":[{"name":"John","parent":{"$ref":"$"}},{"name":"Jane","parent":{"$ref":"$"}}],"childrenCopy":{"$ref":"$['children']"},"child1":{"$ref":"$['children'][0]"},"child2":{"$ref":"$['children'][1]"}}
`
jsonPath reference with preserveArrays = false and jsonPathFormat = Bracket-Notation
`
const JSPON = require('jspon');
JSPON.setSettings({ preserveArrays: false, jsonPathFormat: 'BracketNotation' });
var json = JSPON.stringify(getObjWithCircularRef());
var obj = JSPON.parse(json);
//Value of json variable
//{"name":"parent","children":[{"name":"John","parent":{"$ref":"$"}},{"name":"Jane","parent":{"$ref":"$"}}],"childrenCopy":[{"$ref":"$['children'][0]"},{"$ref":"$['children'][1]"}],"child1":{"$ref":"$['children'][0]"},"child2":{"$ref":"$['children'][1]"}}
``