schangxiang@126.com
2025-09-18 49a51c068d62084bc4c3e77c4be94a20de556c4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# serialize-json
A serialize algorithm for JSON
 
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
 
[npm-image]: https://img.shields.io/npm/v/serialize-json.svg?style=flat-square
[npm-url]: https://npmjs.org/package/serialize-json
[travis-image]: https://img.shields.io/travis/node-modules/serialize-json.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/serialize-json
[codecov-image]: https://codecov.io/gh/node-modules/serialize-json/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/serialize-json
[david-image]: https://img.shields.io/david/node-modules/serialize-json.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/serialize-json
[snyk-image]: https://snyk.io/test/npm/serialize-json/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/serialize-json
[download-image]: https://img.shields.io/npm/dm/serialize-json.svg?style=flat-square
[download-url]: https://npmjs.org/package/serialize-json
 
## Introduction
 
- Support serialize JSON to Buffer, and deserialize Buffer to JSON
  - [√] Boolean
  - [√] String
  - [√] Number
  - [√] Null
  - [√] Undefined
  - [√] Date
  - [√] Buffer
  - [√] Error
  - [√] Plain Object
  - [×] Function
  - [×] RegExp
  - [×] Symbol
 
- Inspire by [jsonpack](https://github.com/sapienlab/jsonpack), it can compress to 55% of original size if the data has a recursive structure
 
## Install
 
```bash
$ npm install serialize-json --save
```
 
Node.js >= 4.0.0 required
 
## Usage
 
```js
let json = {
  a: 'a',
  b: 123,
  c: 123.456,
  d: [ 1, 2, 3 ],
  e: true,
  f: null,
  g: undefined,
  h: new Date(),
  i: new Buffer('this is a buffer'),
  j: new Error('this is a error'),
};
const buf = JSON.encode(json);
const result = JSON.decode(buf);
assert.deepEqual(result, json);
```
 
## API
 
- `encode(json)` serialize a json object
- `decode(buf)` deserialize a buffer to json