Parse JSON safely without throwing
var safeParse = require("safe-json-parse/callback")
safeParse("{}", function (err, json) {
/* we have json */
})
safeparse("WRONG", function (err) {
/* we have err! */
})
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
}
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
}
npm install safe-json-parse
- Raynos