Skip to content

BristolPound/JSPON-For-JavaScript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

If you have seen the following error "TypeError: Converting circular structure to JSON" or something similar, you are in the right place. JSON by default can't process circular structures in objects. This code puts a wrapper around your JSON framework of choice and helps the JSON framework process cyclic objects. It will work with non-cyclic objects also. This code does not interfere with the JSON parser or stringifier; it does it's work outside those actions. It will use the standard JSON.parse and JSON.stringify by default unless you set it to use a different JSON framework.

This code offers a lot of flexibility while maintaining excellent speed (see Performance Results below). It supports id referencing and JSONPath referencing. It can produce a smaller JSON string than regular JSON. This code will work with all the JSON.net preserve reference options. It has been tested and will work in the following Firefox, >= IE8, Chrome, and Nodejs.

Performance Results (using Benchmark.js)

Chrome 46 1 Mock Object jspon#js (Id Referencing) x 14,090 ops/sec ±0.64% (96 runs sampled)
jspon#js (JsonPath Referencing) x 15,764 ops/sec ±0.43% (99 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 15,913 ops/sec ±0.49% (99 runs sampled)
dojo#js (JsonPath Referencing) x 2,744 ops/sec ±1.68% (98 runs sampled)
Fastest: Douglas Crockford's cycle.js
20 Mock Objects jspon#js (Id Referencing) x 792 ops/sec ±0.47% (97 runs sampled)
jspon#js (JsonPath Referencing) x 889 ops/sec ±0.79% (97 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 624 ops/sec ±0.84% (96 runs sampled)
dojo#js (JsonPath Referencing) x 155 ops/sec ±0.75% (81 runs sampled)
Fastest: jspon.js
IE 11 1 Mock Objects jspon#js (Id Referencing) x 9,730 ops/sec ±1.34% (94 runs sampled)
jspon#js (JsonPath Referencing) x 10,842 ops/sec ±2.14% (90 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 6,740 ops/sec ±1.74% (90 runs sampled)
dojo#js (JsonPath Referencing) x 2,272 ops/sec ±2.45% (90 runs sampled)
Fastest: jspon.js
20 Mock Objects jspon#js (Id Referencing) x 504 ops/sec ±1.94% (89 runs sampled)
jspon#js (JsonPath Referencing) x 571 ops/sec ±2.33% (87 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 213 ops/sec ±2.42% (85 runs sampled)
dojo#js (JsonPath Referencing) x 131 ops/sec ±2.47% (76 runs sampled)
Fastest: jspon.js
IE 8 1 Mock Object jspon#js (Id Referencing) x 4,710 ops/sec ±7.15% (58 runs sampled)
jspon#js (JsonPath Referencing) x 5,221 ops/sec ±1.57% (65 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 3,482 ops/sec ±1.72% (22 runs sampled)
dojo#js (JsonPath Referencing) x 1,579 ops/sec ±2.71% (62 runs sampled)
Fastest: jspon.js
20 Mock Objects jspon#js (Id Referencing) x 288 ops/sec ±2.38% (59 runs sampled)
jspon#js (JsonPath Referencing) x 257 ops/sec ±2.82% (59 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 129 ops/sec ±1.98% (58 runs sampled)
dojo#js (JsonPath Referencing) x 94.27 ops/sec ±3.13% (56 runs sampled)
Fastest: jspon.js
Firefox 42 1 Mock Object jspon#js (Id Referencing) x 6,956 ops/sec ±2.09% (92 runs sampled)
jspon#js (JsonPath Referencing) x 9,258 ops/sec ±1.52% (96 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 6,830 ops/sec ±1.53% (92 runs sampled)
dojo#js (JsonPath Referencing) x 1,939 ops/sec ±2.95% (82 runs sampled)
Fastest: jspon.js
20 Mock Objects jspon#js (Id Referencing) x 391 ops/sec ±2.59% (87 runs sampled)
jspon#js (JsonPath Referencing) x 528 ops/sec ±1.91% (91 runs sampled)
Douglas Crockford's cycle#js (JsonPath Referencing) x 251 ops/sec ±0.99% (92 runs sampled)
dojo#js (JsonPath Referencing) x 103 ops/sec ±1.91% (76 runs sampled)
Fastest: jspon.js

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

Convert JSON string to object:

var obj = JSPON.parse(strJSON);

Convert object to JSON string:

var strJson = JSPON.stringify(obj);

Parse JSON string from JSON.net with preserve reference option turned on:

JSPON.setSettings({idFieldName:'$id'}); //somewhere at the top of the page only needs to be set once.
...
...
...
var obj = JSPON.parse(jsonNetPreserveReferenceJSONStr);

About

JSPON for JavaScript. Helps JSON process circular references.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 86.7%
  • HTML 13.3%