safe-json-parse
Parse JSON safely without throwing
Last updated 10 years ago by raynos .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install safe-json-parse 
SYNC missed versions from official npm registry.

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.ifErr(result3)) {
    var err = Result.Err(result3)
    // handle err
} else if (Result.ifOk(result3)) {
    var json = Result.Ok(result3)
    // handle json
}

Installation

npm install safe-json-parse

Contributors

  • Raynos

MIT Licenced

Current Tags

  • 4.0.0                                ...           latest (10 years ago)

4 Versions

  • 4.0.0                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.0.1                                ...           11 years ago
  • 0.1.1                                ...           11 years ago
Maintainers (1)
Downloads
Total 5
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (1)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE