53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
var cycle = require('./cycle');
|
|
|
|
exports.stringify = function stringify(value, replacer, space, _options) {
|
|
|
|
if (arguments.length < 4) {
|
|
try {
|
|
if (arguments.length === 1) {
|
|
return JSON.stringify(value);
|
|
} else {
|
|
return JSON.stringify.apply(JSON, arguments);
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
var options = _options || false;
|
|
if (typeof options === 'boolean') {
|
|
options = {
|
|
'date': options,
|
|
'function': options,
|
|
'regex': options,
|
|
'undefined': options,
|
|
'error': options,
|
|
'symbol': options,
|
|
'map': options,
|
|
'set': options,
|
|
'nan': options,
|
|
'infinity': options
|
|
}
|
|
}
|
|
|
|
var decycled = cycle.decycle(value, options, replacer);
|
|
if (arguments.length === 1) {
|
|
return JSON.stringify(decycled);
|
|
} else {
|
|
return JSON.stringify(decycled, replacer, space);
|
|
}
|
|
|
|
}
|
|
|
|
exports.parse = function parse(text, reviver) {
|
|
var needsRetrocycle = /"\$jsan"/.test(text);
|
|
var parsed;
|
|
if (arguments.length === 1) {
|
|
parsed = JSON.parse(text);
|
|
} else {
|
|
parsed = JSON.parse(text, reviver);
|
|
}
|
|
if (needsRetrocycle) {
|
|
parsed = cycle.retrocycle(parsed);
|
|
}
|
|
return parsed;
|
|
}
|