GithubHelp home page GithubHelp logo

chipincode / safejson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evanshortiss/safejson

0.0 2.0 0.0 4.85 MB

Library to handle JSON.parse/stringify in a standard manner without the need for you to litter a codebase with try catch.

safejson's Introduction

safejson

Concise library to parse and stringify JSON without the need for try catch. Simply use the standard pattern of providing a function with parameters and a callback that takes an error as the first parameter and result as the second.

Browser Support

browser support

Install

npm install safejson --save
bower install safejson --save

Example (Parse)

var VALID_JSON_STRING = JSON.stringify(VALID_OBJECT);


safejson.parse(VALID_JSON_STRING, function(err, json) {
  // err is null as no error would have occured due to valid input
  // json is a valid JSON object
});

Example (Stringify)

// Valid JSON object that will stringify
var VALID_OBJECT = {
  name: 'evan',
  age: 23
};

// Invalid JSON object, has a circular reference
var CIRCULAR_OBJECT = {
  name: 'evan',
  age: 23
};
CIRCULAR_OBJECT.cref = CIRCULAR_OBJECT;

safejson.stringify(VALID_OBJECT, function(err, json) {
  // err would be null as the object is valid json
  // json is a valid json string
});

safejson.stringify(CIRCULAR_OBJECT, function(err, str) {
  // err would be defined as the object contained a circular reference
  // str would equal null
});

Configs

safejson.defer = {Boolean}

If true the parsing of JSON will be briefly deffered. This uses process.nextTick in Node.js and the appropriate browser shim (setTimeout for example) where necessary.

Methods

safejson.stringify(value[, replacer [, space]], callback)

Does the job of JSON.stringify but handles exceptions for you. Supports all the usual JSON.stringify parameters, including the optional replacer and spaces. The last parameter must always be a callback function and is not optional.

safejson.parse(str[, reviver], callback)

Does the job of JSON.parse but handles exceptions for you. Supports all the usual JSON.parse parameters. The last parameter must always be a callback function and is not optional.

safejson's People

Contributors

evanshortiss avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.