buffer.toJSON()

buf.toJSON()

Returns a JSON representation of the Buffer instance. JSON.stringify() implicitly calls this function when stringifying a Buffer instance.

Example:

const buf = Buffer.from('test');
const json = JSON.stringify(buf);

console.log(json);
// Prints: '{"type":"Buffer","data":[116,101,115,116]}'

const copy = JSON.parse(json, (key, value) => {
    return value && value.type === 'Buffer'
      ? Buffer.from(value.data)
      : value;
  });

console.log(copy.toString());
// Prints: 'test'
doc_Nodejs
2016-04-30 04:37:44
Comments
Leave a Comment

Please login to continue.