You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

49 lines
1.2 KiB

var bops = require('bops')
//TODO: handle reviver/dehydrate function like normal
//and handle indentation, like normal.
//if anyone needs this... please send pull request.
exports.stringify = function stringify (o) {
if(o && bops.is(o))
return JSON.stringify(':base64:' + bops.to(o, 'base64'))
if(o && o.toJSON)
o = o.toJSON()
if(o && 'object' === typeof o) {
var s = ''
var array = Array.isArray(o)
s = array ? '[' : '{'
var first = true
for(var k in o) {
var isFunction = 'function' == typeof o[k]
if(Object.hasOwnProperty.call(o, k) && o[k] !== void(0) && !isFunction) {
if(!first)
s += ','
first = false
s += array ? stringify(o[k]) : stringify(k) + ':' + stringify(o[k])
}
}
s += array ? ']' : '}'
return s
} else if ('string' === typeof o) {
return JSON.stringify(/^:/.test(o) ? ':' + o : o)
} else
return JSON.stringify(o)
}
exports.parse = function (s) {
return JSON.parse(s, function (key, value) {
if('string' === typeof value) {
if(/^:base64:/.test(value))
return bops.from(value.substring(8), 'base64')
else
return /^:/.test(value) ? value.substring(1) : value
}
return value
})
}