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.

43 lines
961 B

12 years ago
var Buffer = require('buffer').Buffer
12 years ago
//TODO: handle reviver/dehydrate function like normal
//and handle indentation, like normal.
//if anyone needs this... please send pull request.
12 years ago
12 years ago
exports.stringify = function stringify (o) {
12 years ago
if(o && Buffer.isBuffer(o))
o = o.toString('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) {
if(Object.hasOwnProperty.call(o, k)) {
if(!first)
s += ', '
first = false
s += array ? stringify(o[k]) : stringify(k) + ': ' + stringify(o[k])
}
}
s += array ? ']' : '}'
return s
} else
return JSON.stringify(o)
}
exports.parse = function (s) {
return JSON.parse(s, function (key, value) {
if('string' === typeof value && /==$/.test(value))
return new Buffer(value, 'base64')
return value
})
}