Parse JSON safely without throwing
JavaScript HTML
Latest commit 364044b May 29, 2016 @Raynos Merge pull request #3 from world/master
examples using rust-result had several typos
Permalink
Failed to load latest commit information.
examples initial May 27, 2013
test switch over to rust-result Feb 23, 2015
.gitignore initial May 27, 2013
.testem.json initial May 27, 2013
.travis.yml initial May 27, 2013
LICENCE initial May 27, 2013
README.md examples using rust-result had several typos May 28, 2016
callback.js implement tuple api Jun 26, 2014
package.json 4.0.0 Feb 23, 2015
result.js switch over to rust-result Feb 23, 2015
tuple.js implement tuple api Jun 26, 2014

README.md

safe-json-parse

build status dependency status

Parse JSON safely without throwing

Example (callback)

var safeParse = require("safe-json-parse/callback")

safeParse("{}", function (err, json) {
    /* we have json */
})

safeparse("WRONG", function (err) {
    /* we have err! */
})

Example (tuple)

var safeParse = require("safe-json-parse/tuple")

var tuple1 = safeParse("{}")
var json = tuple1[1] /* we have json */

var tuple2 = safeparse("WRONG")
var err = tuple2[0] /* we have err! */

var tuple3 = safeParse(something)
if (tuple3[0]) {
    var err = tuple3[0]
    // handle err
} else {
    var json = tuple3[1]
    // handle json
}

Example (result)

var Result = require('rust-result')
var safeParse = require('safe-json-parse/result')

var result1 = safeParse("{}")
var json = Result.Ok(result1) /* we have json */

var result2 = safeparse("WRONG")
var err = Result.Err(result2) /* we have err! */

var result3 = safeParse(something)
if (Result.isErr(result3)) {
    var err = Result.Err(result3)
    // handle err
} else if (Result.isOk(result3)) {
    var json = Result.Ok(result3)
    // handle json
}

Installation

npm install safe-json-parse

Contributors

  • Raynos

MIT Licenced