Home  >  Article  >  Web Front-end  >  node.js中的buffer.toJSON方法使用说明_node.js

node.js中的buffer.toJSON方法使用说明_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:201630browse

方法说明:

将buffer对象 转换成json格式。

语法:

复制代码 代码如下:

buffer.toJSON()

接收参数:

例子:

复制代码 代码如下:

var buf = new Buffer('test');
var json = JSON.stringify(buf);
console.log(json);
// '{"type":"Buffer","data":[116,101,115,116]}'
var copy = JSON.parse(json, function(key, value) {
    return value && value.type === 'Buffer'
      ? new Buffer(value.data)
      : value;
  });
console.log(copy);
//

源码:

复制代码 代码如下:

Buffer.prototype.toJSON = function() {
  return {
    type: 'Buffer',
    data: Array.prototype.slice.call(this, 0)
  };
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn