Extend JSON.stringify() & JSON.parse() to handle more values:
- Date
undefined
- Function
- RegExp
- Getter
- Setter
And both stringify()
and parse()
are safe:
-
No modification on original data.
-
No special markup on special values. Auxiliary data structure is used to mark special values.
For example, if we mark Date instance with prefix
__DATE__
, it may conflicts with real values in some cases. -
Identical before
stringify()
and afterparse()
.
npm i --save json-enhance
import { stringify, parse } from 'json-enhance'
const obj = {
a: 1,
b: true,
c: [
'123',
new Date(2018, 0, 1),
{ x: '123' },
(a, b) => a - b,
],
d: undefined,
e: /\/(.*?)\/([gimy])?$/,
f: (a, b) => a + b,
}
Object.defineProperty(obj, 'x', {
get() {
return this.mX
},
set(v) {
this.mX = v
},
})
const str = stringify(obj)
parse(str) // equals obj
Mainly used for state recoverage of web app. See my article here.
Functions, RegExp, Getter, Setter are compared by calling toString()
and then direct string matching.
If functions access variables in the upper closure, calling them after parse()
may cause error. Keep your functions pure.
There are several ways to write getter/setter, however, one of them is unapplicable to parse()
:
✅
Object.defineProperty(obj, 'x', {
get() {
return this.mX
},
set(v) {
this.mX = v
},
})
✅
obj.__defineGetter__('x', function () {
return this.mX
})
obj.__defineSetter__('x', function (v) {
this.mX = v
})
❌
const obj = {
get x() {
return this.mX
}
set x(v) {
this.mX = v
}
}