Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JSON.stringify() function converts buffers into objects. The raw data is encoded as an array of bytes that you can pass in to Buffer.from().

let buf = Buffer.from('Hello, World', 'utf8');

let obj = { buffer: buf };
obj = JSON.parse(JSON.stringify(obj));

// { type: 'Buffer',
//   data: [ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100 ] }
obj.buffer;

// To convert from JSON representation back to a buffer, use `Buffer.from()`
obj.buffer = Buffer.from(obj.buffer);
obj.buffer.toString('utf8'); // 'Hello, World'
Source by masteringjs.io #
 
PREVIOUS NEXT
Tagged: #function #converts #buffers #The #raw #data #encoded #array #bytes #pass
ADD COMMENT
Topic
Name
2+4 =