0.2.0 • Published 7 years ago

json-ex v0.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

json-ex

Build Status Dependency Status devDependency Status Coverage Status npm npm

Extends JSON to be able to serialize and deserialize more than just basic primitives

What is this?

Let's parse JSON like it is 20**! Let's add support for functions, buffers, dates and the basic primitives! If you know of anymore let me know and let's add them!

This is not a recreation of JSON, it is extending the protocol to allow for more types

Install

npm install json-ex

Usage

json-ex is a drop in replacement for JSON, so it implements JSON.stringify and JSON.parse

Stringify

const JSONex = require('json-ex');
const object = {
    name: 'Hello world',
    person: true,
    age: 100000,
    buffer: new Buffer('hi'),
    date: new Date('10/20/2017'),
    func: function hello() { return 'hello world' },
    reg: new RegExp('%name%')
};
const output = JSONex.stringify(object);

output

{
  "name": "Hello world",
  "person": true,
  "age": 100000,
  "buffer": "_BuffEx_%7B%22type%22%3A%22Buffer%22%2C%22data%22%3A%5B104%2C105%5D%7D",
  "date": "_DateEx_2017-10-20T07%3A00%3A00.000Z",
  "func": "_FuncRa_function%20hello()%20%7B%20return%20'hello%20world'%20%7D",
  "reg": "_PxEgEr_%5B%22%25name%25%22%2C%22%22%5D"
}```

### Parse

```javascript
const string = `{
  "name": "Hello world",
  "person": true,
  "age": 100000,
  "buffer": "_BuffEx_%7B%22type%22%3A%22Buffer%22%2C%22data%22%3A%5B104%2C105%5D%7D",
  "date": "_DateEx_2017-10-20T07%3A00%3A00.000Z",
  "func": "_FuncRa_function%20hello()%20%7B%20return%20'hello%20world'%20%7D",
  "reg": "_PxEgEr_%5B%22%25name%25%22%2C%22%22%5D"
}`;
const output = JSONex.parse(string);

output

{ name: 'Hello world',
  person: true,
  age: 100000,
  buffer: <Buffer 68 69>,
  date: 2017-10-20T07:00:00.000Z,
  func: [Function: hello],
  reg: /%name%/
}

Benchmark

run benchmarks by running npm run benchmark

json-ex
  41,763 op/s » Stringify
 87,266 op/s » Parse


Suites:  1
Benches: 2
Elapsed: 1,760.74 ms