https://github.com/rreverser/serialize-js
Object serialization for JavaScript - readable & JS-compatible
https://github.com/rreverser/serialize-js
Last synced: 9 months ago
JSON representation
Object serialization for JavaScript - readable & JS-compatible
- Host: GitHub
- URL: https://github.com/rreverser/serialize-js
- Owner: RReverser
- License: mit
- Created: 2014-07-22T10:35:57.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2016-12-18T15:45:37.000Z (over 9 years ago)
- Last Synced: 2025-09-01T00:19:57.504Z (9 months ago)
- Language: JavaScript
- Homepage: https://npmjs.org/package/serialize-js
- Size: 176 KB
- Stars: 29
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
serialize-js
============
[](https://travis-ci.org/RReverser/serialize-js)
## What's this?
Small serialization helper for those who wants to get JS representation of object but gets only this dirty JSON.
## Why does it exist?
Sometimes it's useful to serialize object into the JS user-readable representation but the only option you have is JSON which adds all this damn quotes around any keys (incl. valid identifiers), indents entire contents of any objects/arrays etc.
This small serializer allows you to overcome that and get pretty representations, just as you would write it with own hands in code:
objJSON.stringify(obj, null, 2)serialize(obj)
{a: 1}
{
"a": 1
}
{a: 1}
{a: 1, b: 2}
{
"a": 1,
"b": 2
}
{a: 1, b: 2}
{a: 1, b: 2, c: 3}
{
"a": 1,
"b": 2,
"c": 3
}
{
a: 1,
b: 2,
c: 3
}
[{a: 1, b: 2, c: 3, '-': '+'}]
[
{
"a": 1,
"b": 2,
"c": 3,
"-": "+"
}
]
[{
a: 1,
b: 2,
c: 3,
"-": "+"
}]
[{a: 1}, {b: 2}]
[
{
"a": 1
},
{
"b": 2
}
]
[
{a: 1},
{b: 2}
]
## How can I customize the output?
You can optionally pass options object as second argument (`serialize(obj, { /*...options...*/ })`).
Possible options are below:
### initialIndent
Type: `Number|String`
Default: `''`
Initial indentation of output (generated indentation will be relative to this one). It can be either number of spaces or explicit string.
### indent
Type: `Number|String`
Default: `2`
Indentation to be used for nested representations. It can be either number of spaces or explicit string (like `'\t'`).
### forceJSON
Type: `Boolean`
Default: `false`
If set to `true`, generates JSON-compatible output (all the keys are wrapped with quotes, but indentation is still optimized).