📌  相关文章
📜  JSON.stringify() 函数将缓冲区转换为对象.原始数据被编码为一个字节数组,您可以将其传递给 Buffer.from(). - Javascript代码示例

📅  最后修改于: 2022-03-11 15:02:23.913000             🧑  作者: Mango

代码示例1
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'