207681893ea3c51b4f3ca4241e7afba32288684b3c4de7b5bed8737c21fd6a461688b8d85c42493b227ca3b07386d6f9d342853241dc9388c656fac979a161 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # buffer-json
  2. ```
  3. npm install buffer-json
  4. ```
  5. ```js
  6. const BJSON = require('buffer-json')
  7. const str = BJSON.stringify({ buf: Buffer.from('hello') })
  8. // => '{"buf":{"type":"Buffer","data":"base64:aGVsbG8="}}'
  9. BJSON.parse(str)
  10. // => { buf: <Buffer 68 65 6c 6c 6f> }
  11. ```
  12. The [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) class in Node.js is used to represent binary data. JSON does not specify a way to encode binary data, so the Node.js implementation of `JSON.stringify` represents buffers as an object of shape `{ type: "Buffer", data: [<bytes as numbers>] }`. Unfortunately, `JSON.parse` does not turn this structure back into a `Buffer` object:
  13. ```
  14. $ node
  15. > JSON.parse(JSON.stringify({ buf: Buffer.from('hello world') }))
  16. { buf:
  17. { type: 'Buffer',
  18. data: [ 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ] } }
  19. ```
  20. `JSON.stringify` and `JSON.parse` accept arguments called [`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) and [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) respectively which allow customizing the parsing/encoding behavior. This module provides a replacer which encodes Buffer data as a base64-encoded string, and a reviver which turns JSON objects which contain buffer-like data (either as arrays of numbers or strings) into `Buffer` instances. All other types of values are parsed/encoded as normal.
  21. ## API
  22. ### `stringify(value[, space])`
  23. Convenience wrapper for [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) with the `replacer` described below.
  24. ### `parse(text)`
  25. Convenience wrapper for [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) with the `reviver` described below.
  26. ### `replacer(key, value)`
  27. A [`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) implementation which turns every value that is a `Buffer` instance into an object of shape `{ type: 'Buffer', data: 'base64:<base64-encoded buffer content>' }`. Empty buffers are encoded as `{ type: 'Buffer', data: '' }`.
  28. ### `reviver(key, value)`
  29. A [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) implementation which turns every object of shape `{ type: 'Buffer', data: <array of numbers or string> }` into a `Buffer` instance.
  30. ## Related modules
  31. - [`buffer-json-encoding`](https://github.com/lachenmayer/buffer-json-encoding): an [`abstract-encoding`](https://github.com/mafintosh/abstract-encoding) compatible JSON encoder/decoder which uses this module.
  32. ## License
  33. MIT