Coder Social home page Coder Social logo

easy-json-parse's Introduction

easy-json-parse

Are you still worried about the long code with try {} catch {}? like this:

const jsonString = 'easy';
let json;

try {
  json = JSON.parse(jsonString);
} catch (e) {
  json = {};
}

console.log(json.xxx);

or

const jsonString = null;
let json;

try {
  json = JSON.parse(jsonString) || {};
} catch (e) {
  json = {};
}

console.log(json.xxx);

Now, no matter whether it is long code or initialization, you don't need to worry.

use easy-json-parse will more easy and safe for try{} catch{}.

Get started

npm i easy-json-parse

case: initialValue

import easyParse from 'easy-json-parse';
const jsonString = 'easy';
const [error, json] = easyParse(jsonString, {initialValue: {}});

console.log(json.xxx); // If json is not exist, will return {} safely.

case: normal

import easyParse from 'easy-json-parse';
const jsonString = '{"easy": "easy"}';
const [error, json] = easyParse(jsonString);

console.log(json.easy); // easy

case: don't need error

import easyParse from 'easy-json-parse';
const jsonString = '{"easy": "easy"}';
const [, json] = easyParse(jsonString);

console.log(json.easy); // easy

Syntax

Parameters

text

the string to parse as JSON

options

options.reviver(optional)

  • if a function, prescribes how the value originally produced by parsing is transformed, before being returned, more info about this param in here

options.initialValue(optional)

  • init value for json.

Returns

Array [error, value]

License

MIT

Copyright (c) 2019 蓝色的秋风

easy-json-parse's People

Contributors

hua1995116 avatar meloguo avatar

Watchers

 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.