Coder Social home page Coder Social logo

json-expressible's Introduction

CI-CD

JSON-Expressible

An ES5 Javascript micro library that determines whether a value can be expressed as JSON.

  • Checks whether the value is a valid JSON type (string, number, boolean, null, object, or array).

  • Recursively checks whether object children and/or array elements are valid JSON types.

  • Checks whether there are any circular references.

The motivation for this library is twofold:

  1. The JSON.stringify() function does not throw an error when it encounters non-JSON values like undefined and NaN. Instead, it omits the values or converts them to null in the returned serialization. As a result, parsing the serialization does not return the original value.

  2. It can be useful to know whether a value is expressible in JSON without actually serializing it.

To install:

npm install json-expressible

To use:

var jsonExpressible = require("json-expressible");

// The following return true

console.log(jsonExpressible("abc"));
console.log(jsonExpressible(123));
console.log(jsonExpressible(true));
console.log(jsonExpressible(null));
console.log(jsonExpressible({ abc: "def" }));
console.log(jsonExpressible([1, 2, 3]));


// The following return false

console.log(jsonExpressible(undefined));
console.log(jsonExpressible(NaN));
console.log(jsonExpressible(function () {}));
console.log(jsonExpressible(new Date()));
console.log(jsonExpressible(Infinity));
console.log(jsonExpressible(/reg.ex/));

console.log(jsonExpressible([ undefined ]));
console.log(jsonExpressible({ abc: undefined }));

var circularObject = {};
circularObject.ref = circularObject;
console.log(jsonExpressible(circularObject));

var circularArray = [];
circularArray.push(circularArray);
console.log(jsonExpressible(circularArray));

json-expressible's People

Contributors

aarong avatar

Watchers

 avatar James Cloos 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.