├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | node_modules/* 3 | npm_debug.log 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Dominic Tarr 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json-buffer 2 | 3 | JSON functions that can convert buffers! 4 | 5 | [![build status](https://secure.travis-ci.org/dominictarr/json-buffer.png)](http://travis-ci.org/dominictarr/json-buffer) 6 | 7 | [![testling badge](https://ci.testling.com/dominictarr/json-buffer.png)](https://ci.testling.com/dominictarr/json-buffer) 8 | 9 | JSON mangles buffers by converting to an array... 10 | which isn't helpful. json-buffers converts to base64 instead, 11 | and deconverts base64 to a buffer. 12 | 13 | ``` js 14 | var JSONB = require('json-buffer') 15 | var Buffer = require('buffer').Buffer 16 | 17 | var str = JSONB.stringify(Buffer.from('hello there!')) 18 | 19 | console.log(JSONB.parse(str)) //GET a BUFFER back 20 | ``` 21 | 22 | ## License 23 | 24 | MIT 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | //TODO: handle reviver/dehydrate function like normal 2 | //and handle indentation, like normal. 3 | //if anyone needs this... please send pull request. 4 | 5 | exports.stringify = function stringify (o) { 6 | if('undefined' == typeof o) return o 7 | 8 | if(o && Buffer.isBuffer(o)) 9 | return JSON.stringify(':base64:' + o.toString('base64')) 10 | 11 | if(o && o.toJSON) 12 | o = o.toJSON() 13 | 14 | if(o && 'object' === typeof o) { 15 | var s = '' 16 | var array = Array.isArray(o) 17 | s = array ? '[' : '{' 18 | var first = true 19 | 20 | for(var k in o) { 21 | var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k]) 22 | if(Object.hasOwnProperty.call(o, k) && !ignore) { 23 | if(!first) 24 | s += ',' 25 | first = false 26 | if (array) { 27 | if(o[k] == undefined) 28 | s += 'null' 29 | else 30 | s += stringify(o[k]) 31 | } else if (o[k] !== void(0)) { 32 | s += stringify(k) + ':' + stringify(o[k]) 33 | } 34 | } 35 | } 36 | 37 | s += array ? ']' : '}' 38 | 39 | return s 40 | } else if ('string' === typeof o) { 41 | return JSON.stringify(/^:/.test(o) ? ':' + o : o) 42 | } else if ('undefined' === typeof o) { 43 | return 'null'; 44 | } else 45 | return JSON.stringify(o) 46 | } 47 | 48 | exports.parse = function (s) { 49 | return JSON.parse(s, function (key, value) { 50 | if('string' === typeof value) { 51 | if(/^:base64:/.test(value)) 52 | return Buffer.from(value.substring(8), 'base64') 53 | else 54 | return /^:/.test(value) ? value.substring(1) : value 55 | } 56 | return value 57 | }) 58 | } 59 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-buffer", 3 | "description": "JSON parse & stringify that supports binary via bops & base64", 4 | "version": "3.0.1", 5 | "homepage": "https://github.com/dominictarr/json-buffer", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dominictarr/json-buffer.git" 9 | }, 10 | "devDependencies": { 11 | "tape": "^4.6.3" 12 | }, 13 | "scripts": { 14 | "test": "set -e; for t in test/*.js; do node $t; done" 15 | }, 16 | "author": "Dominic Tarr (http://dominictarr.com)", 17 | "license": "MIT", 18 | "testling": { 19 | "files": "test/*.js", 20 | "browsers": [ 21 | "ie/8..latest", 22 | "firefox/17..latest", 23 | "firefox/nightly", 24 | "chrome/22..latest", 25 | "chrome/canary", 26 | "opera/12..latest", 27 | "opera/next", 28 | "safari/5.1..latest", 29 | "ipad/6.0..latest", 30 | "iphone/6.0..latest", 31 | "android-browser/4.2..latest" 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | var test = require('tape') 3 | var _JSON = require('../') 4 | 5 | function clone (o) { 6 | return JSON.parse(JSON.stringify(o)) 7 | } 8 | 9 | var examples = { 10 | simple: { foo: [], bar: {}, baz: Buffer.from('some binary data') }, 11 | just_buffer: Buffer.from('JUST A BUFFER'), 12 | all_types: { 13 | string:'hello', 14 | number: 3145, 15 | null: null, 16 | object: {}, 17 | array: [], 18 | boolean: true, 19 | boolean2: false 20 | }, 21 | foo: Buffer.from('foo'), 22 | foo2: Buffer.from('foo2'), 23 | escape: { 24 | buffer: Buffer.from('x'), 25 | string: _JSON.stringify(Buffer.from('x')) 26 | }, 27 | escape2: { 28 | buffer: Buffer.from('x'), 29 | string: ':base64:'+ Buffer.from('x').toString('base64') 30 | }, 31 | undefined: { 32 | empty: undefined, test: true 33 | }, 34 | undefined2: { 35 | first: 1, empty: undefined, test: true 36 | }, 37 | undefinedArray: { 38 | array: [undefined, 1, 'two'] 39 | }, 40 | fn: { 41 | fn: function () {} 42 | }, 43 | undefined: undefined 44 | } 45 | 46 | for(k in examples) 47 | (function (value, k) { 48 | test(k, function (t) { 49 | var s = _JSON.stringify(value) 50 | console.log('parse', s) 51 | if(JSON.stringify(value) !== undefined) { 52 | console.log(s) 53 | var _value = _JSON.parse(s) 54 | t.deepEqual(clone(_value), clone(value)) 55 | } 56 | else 57 | t.equal(s, undefined) 58 | t.end() 59 | }) 60 | })(examples[k], k) 61 | 62 | 63 | 64 | --------------------------------------------------------------------------------