safejson
Concise library to parse and stringify JSON without the need for try catch. Simply use the standard Node.js pattern of providing parameters and a callback that takes an error as the first parameter and result as the second.
This library is nice for chaining operations using async as demonstrated below.
Browser Support
Pretty much any browser with JSON support.
Install
npm install safejson --save
bower install safejson --save
Example with Async
var safejson = async = fs = ; exports { async; }
Example (Parse)
// Valid JSON object that will stringifyvar VALID_OBJECT = name: 'evan' age: 23; var VALID_JSON_STRING = JSON; safejson;
Example (Stringify)
// Valid JSON object that will stringifyvar VALID_OBJECT = name: 'evan' age: 23; // Invalid JSON object, has a circular reference added belowvar CIRCULAR_OBJECT = name: 'evan' age: 23;CIRCULAR_OBJECTcref = CIRCULAR_OBJECT; safejson; safejson;
Configs
safejson.defer = {Boolean}
If true the parsing of JSON will be briefly deffered. This uses process.nextTick in Node.js and the appropriate browser shim (setTimeout for example) where necessary.
Methods
safejson.stringify(value[, replacer [, space]], callback)
Does the job of JSON.stringify but handles exceptions for you. Supports all the usual JSON.stringify parameters, including the optional replacer and spaces. The last parameter must always be a callback function and is not optional.
safejson.parse(str[, reviver], callback)
Does the job of JSON.parse but handles exceptions for you. Supports all the usual JSON.parse parameters. The last parameter must always be a callback function and is not optional.