nsnjson-driver
NSNJSON (Not So Normal JSON) Driver
Last updated 5 years ago by jxcoder .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install nsnjson-driver 
SYNC missed versions from official npm registry.

NSNJSON Node.js Driver build

NSNJSON - Not So Normal JSON

nsnjson_logo

Why?

Have you ever think about JSON with only 4 types: number, string, array, object? :)

What if your programming language or some another ecosystem doesn't support null or true/false as a types or a special values?

If it is your case then NSNJSON comes to help you! :)

The NSNJSON operates only 4 types - number, string, array, object!

Also, it defines types mapping table:

JSON type null number string true false array object
NSNJSON type 0 1 2 3 3 4 5

How?

For every JSON type NSNJSON defines special presentation which is absolutely valid JSON.

NSNJSON presentation is always JSON object.

Such object always contains field "t" (which means type of source JSON type).

Also, for all JSON types except null the object always contains field "v" (which means value of source JSON type).

JSON object field has a name. So, NSNJSON saves this information in field "n" (which means name of field JSON object).

More info on NSNJSON Driver page.

Install

NSNJSON Node.js Driver is now available through NPM. So,

npm install nsnjson-driver

Usage

The driver uses data.maybe as a wrapper for encoding / decoding results.

var nsnjson = require('nsnjson-driver');

var data = null;

var presentationMaybe = nsnjson.encode(data);

console.log(presentationMaybe);
// Maybe { value: { "t": 0 } }

console.log(nsnjson.decode(presentationMaybe.get()));
// Maybe { value: null }

Custom rules

You can define your own rules for JSON encoding/decoding.

Just pass custom rules as an argument to related functions:

  • nsnjson.driver @ encode(JSON, options)
  • nsnjson.driver @ decode(NSNJSON, options)
  • nsnjson.encoder @ encode(JSON, options)
  • nsnjson.decoder @ decode(NSNJSON, options)

Example:

var Maybe = require('data.maybe');

var nsnjson = require('nsnjson-driver');

var data = 1007;

var presentationMaybe = nsnjson.encode(data, {
  'number': function(value) {
    return Maybe.Just(['number', value]);
  }
});

console.log(presentationMaybe);

// Maybe { value: [ "number", 1007 ] }

console.log(nsnjson.decode(presentationMaybe.get(), {
  'type': function(presentation) {
    return Maybe.Just(presentation[0]);
  },
  'number': function(presentation) {
    return Maybe.Just(presentation[1]);
  }
}));

// Maybe { value: 1007 }

Current Tags

  • 0.0.7                                ...           latest (5 years ago)

7 Versions

  • 0.0.7                                ...           5 years ago
  • 0.0.6                                ...           5 years ago
  • 0.0.5                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |