Package Exports
- tryjson
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tryjson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node tryjson
Why
Not everyone knows that you should always run JSON.parse
inside of the try/catch
block or otherwise you risk your application crashing on bad input. Most of the examples of using JSON.parse
that I see online never does that. People usually assume that you will get undefined
on bad or empty input but you don't.
Remember: Always try { JSON.parse() }
or use tryjson.parse()
This module works like many people assume that the built-in JSON
works and can simplify some common code. People usually write:
object = JSON.parse(string);
when they mean:
try {
object = JSON.parse(string);
} catch (e) {
object = undefined;
}
and now they can write it as:
object = tryjson.parse(string);
or even as:
object = JSON.parse(string);
if you want to locally override JSON
with:
var JSON = require('tryjson);
How it works
This module works like JSON.parse
(and in fact it uses JSON.parse
) but instead of throwing exceptions it returns undefined
on failure. This is not always a desired behaviour but sometimes it is.
There is also a stringify
method that works like JSON.stringify
but instead of throwing exceptions on circular structures it returns "null"
- which, again, may not be what you always want but sometime it is and you can use this module to simplify your code in those cases.
Rationale
Why tryjson.parse
returns undefined
for invalid JSON? Because a valid JSON can never be parsed to undefined
so you can test it reliably for that value with value === undefined
to know if it was invalid.
Why tryjson.stringify
returns "null"
for objects that cannot be serialized? Because "null"
is a valid JSON string so it can always be parsed without errors and is still easy to test for null
value. Note that this time, getting "null" does not necessarily mean that the object couldn't be serialized because it might have been null
as well.
Installation
Install to use in your Node project, updating the dependencies in package.json:
npm install tryjson --save
Examples
Basic usage:
Parsing
var tryjson = require('tryjson');
console.log(tryjson.parse('{"a":1,"b":2}'));
// { a: 1, b: 2 }
console.log(tryjson.parse('{"a":1,"b":2'));
// undefined
Stringification
var tryjson = require('tryjson');
var x = {a: 1};
console.log(tryjson.stringify(x));
// '{"a":1}'
x.b = x;
console.log(tryjson.stringify(x));
// 'null'
Testing returned values
var object = tryjson.parse(string);
if (object === undefined) {
// the string was invalid JSON
}
if (object == null) {
// the string was either invalid JSON or "null"
}
if (!object) {
// the string was either invalid JSON, "null", "false" or "0"
}
Issues
For any bug reports or feature requests please post an issue on GitHub.
Author
Rafał Pocztarski - https://github.com/rsp
License
MIT License (Expat). See LICENSE.md for details.