This package has been deprecated

Author message:

no longer maintained

jsonbase

1.0.0 • Public • Published

jsonbase

Check and transform your JSON data.

Why jsonbase?

jsonbase is mainly built for back-to-front-end data communication. Sometimes we may have the issue that data from the server is not in the right type for JavaScript and some weird problem may happen, like the following example:

This is the JSON data return by server:

{
    "name": "Peter",
    "friends": "18",
    "address": null,
    "confirmed": 1
}

and this is the JSON data we expected:

{
    "name": "Peter",
    "friends": 18,
    "address": "",
    "confirmed": true
}

So here's the problem: the friends field should be a number, not a string. And if we add 1 to it, we will only get '181' other than 19. This has the similar problem with address and confirmed field.

With jsonbase, we can prevent the tragedy by checking and converting data types:

var DataTypes = jsonbase.DataTypes;
var schema = {
    name: DataTypes.string,
    friends: DataTypes.number,
    address: DataTypes.string,
    confirmed: DataType.bool
};
jsonbase.parse(json_data, schema);
/*
 * {
 *     "name": "Peter",
 *     "friends": 18,
 *     "address": "",
 *     "confirmed": true
 * }
 */

Usage

Installation

  • Node:

    npm install jsonbase

  • Other:

    • Just clone the repo and use index.js
    • jsonbase supports AMD & CMD module systems.

Basic jsonbase data types:

Please see test/jsonbase.test.js for details.

  • DataTypes.nul: mark a field as a null,
    • null, 0, '' => null,
    • or a TypeNotMatchError will be thrown.
  • DataTypes.bool: mark a field as a boolean,
    • null, false, 0, 'false' => false,
    • true, 1, 'true' => true,
    • or a TypeNotMatchError will be thrown.
  • DataTypes.number: mark a field as a number,
    • null => 0,
    • true and false => 1 and 0,
    • numbers will remain what they are,
    • and strings consist of pure numbers will converted to the corresponding numbers.
    • You will get a TypeNotMatchError from other data types.
  • DataTypes.string: mark a field as a string,
    • null => '',
    • true => 'true', false => 'false',
    • strings remains what they are,
    • You will get a TypeNotMatchError from other data types.
  • DataTypes.array: mark a field as an array:
    • arrays remains what they are,
    • or a TypeNotMatchError will be thrown.
  • DataTypes.array: mark a field as an object:
    • objects remains what they are,
    • or a TypeNotMatchError will be thrown.

Schema

jsonbase uses schema to validate and convert JSON object. jsonbase supports nested schema so you can use it on complex JSON data:

var DataTypes = jsonbase.DataTypes;
var parsedJSON = {
    "name": "jsonbase",
    "stargazers_count": 0,
    "owner": {
        "id": 1234567890,
        "site_admin": false
    },
    "a_made_up_field": [
        {
            "id": 1,
            "arr": [false, 1, null]
        },
        {
            "id": 2,
            "arr": [true, 2, null]
        }
    ],
};
var schema = {
    name: DataTypes.string,
    stargazers_count: DataTypes.number,
    owner: {
        id: DataTypes.number,
        site_admin: DataTypes.bool
    },
    a_made_up_field: [
        {
            id: DataTypes.number,
            arr: DataTypes.array
        }
    ]
};
jsonbase.parse(parsedJSON, schema);

Package Sidebar

Install

npm i jsonbase

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • ppoffice