try-read-json

Graceful reading of JSON value, using JSON.parse with support for optional callback
Install
Install with npm
$ npm i try-read-json --save
Usage
For more use-cases see the tests
const tryReadJson =
API
tryReadJson
Parses
input
JSON value without throwing an errors.
Params
input
{String|Number|Null|Boolean}: json valuecallback
{Function}: optional callbackreturns
{Undefined|Error}: if something fails and there's nocallback
it returns that Error
Example
var tryReadJson = // synchronousconsole // => 1234console // => 1234console // => [ 'aaa', 'bbb' ]console // => { foo: 'bar' }console // => nullconsole // => trueconsole // => false console // => SyntaxErrorconsole // => SyntaxErrorconsole // => SyntaxError // with callback
Related
- always-done: Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement for [async-done… more | homepage
- then-parse-json: Gracefully parse JSON using promises - promisified JSON.parse | homepage
- to-callback: Converts result-first callbacks to classic (node.js-style) error-first callbacks with 3 lines of code. Useful when you want to promisify result-first APIs (like… more | homepage
- try-catch-callback: try/catch block with a callback, used in try-catch-core. Use it when you don't care about asyncness so much and don't want guarantees… more | homepage
- try-catch-core: Low-level package to handle completion and errors of sync or asynchronous functions, using once and dezalgo libs. Useful for and used in… more | homepage
- write-file: Writing a file to disk, creates intermediate directories in the destination path if they dont already exist. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.