Error catching JSON methods. Hides away the try/catch madness for parsing strings and stringifying JSON objects.
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
README.md
index.js
package.json

README.md

jsun

Error catching JSON methods.

Hides away the try/catch madness for parsing strings and stringifying JSON objects.

Install

npm install jsun --save

Usage

var JSUN = require('jsun');

var validStr = JSUN.stringify({key: 'value'}).string;
var invalidStr = '<div></div>';

var validData = JSUN.parse(validStr);
console.log(validData.json) // {key: 'value'}
console.log(validData.err) // null

var invalidData = JSUN.parse(invalidStr);
console.log(invalidData.json) // null
console.log(invalidData.err) // error message from try/catch

Methods

parse(string)

Uses JSON.parse in a try/catch block

Returns:

{
  json: {} // some parsed string
  err: null // not null if there is an error
}

stringify(object)

Uses JSON.stringify in a try/catch block

Returns:

{
  string: {} // some stringified object
  err: null // not null if there is an error
}

Run Tests

npm install
npm test