パッケージの詳細

try-json-parse

alessioalex30.1kMIT1.0.0

Doesn't throw an error when JSON.parse() fails, just returns undefined

err, custom, custom-error, customerror

readme

try-json-parse

Just like JSON.parse(), except it returns undefined instead of throwing an error for invalid JSON.

Usage

var JSONparse = require('./index'),
    assert    = require('assert'),
    fs        = require('fs');

fs.readFile(__dirname + '/package.json', function(err, content) {
  var pkgJson;

  if (err) { throw err; }

  pkgJson = JSONparse(content);
  assert.equal(pkgJson.name, 'try-json-parse');
  assert(JSONparse(fs.readFileSync(__filename)) === undefined);
});

Motivation

Got tired of writing try {} catch {}.

License

MIT

更新履歴

v1.0.0

  • simplify implementation - by @dsh0416

v0.1.0

  • null is valid json, return undefined - by @Raynos
  • JSON.parse takes an optional "reviver" second parameter - by @humanchimp