Sha256: d7596e0f7c808f2800aa6fa9dfa723c5547ab964aba7dcd9014cebf292f667d1

Contents?: true

Size: 1.47 KB

Versions: 42

Compression:

Stored size: 1.47 KB

Contents

//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('undefined' == typeof o) return o

  if(o && Buffer.isBuffer(o))
    return JSON.stringify(':base64:' + 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) {
      var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k])
      if(Object.hasOwnProperty.call(o, k) && !ignore) {
        if(!first)
          s += ','
        first = false
        if (array) {
          if(o[k] == undefined)
            s += 'null'
          else
            s += stringify(o[k])
        } else if (o[k] !== void(0)) {
          s += stringify(k) + ':' + stringify(o[k])
        }
      }
    }

    s += array ? ']' : '}'

    return s
  } else if ('string' === typeof o) {
    return JSON.stringify(/^:/.test(o) ? ':' + o : o)
  } else if ('undefined' === typeof o) {
    return 'null';
  } 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 Buffer.from(value.substring(8), 'base64')
      else
        return /^:/.test(value) ? value.substring(1) : value 
    }
    return value
  })
}

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
immosquare-cleaner-0.1.52 node_modules/json-buffer/index.js
trusty-cms-6.3.1 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.51 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.50 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.49 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.48 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.47 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.46 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.45 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.44 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.43 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.42 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.41 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.40 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.39 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.38 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.32 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.31 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.30 node_modules/json-buffer/index.js
immosquare-cleaner-0.1.29 node_modules/json-buffer/index.js