Home >Web Front-end >JS Tutorial >Instructions for using the buffer.toJSON method in node.js_node.js

Instructions for using the buffer.toJSON method in node.js_node.js

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

Method description:

Convert buffer object into json format.

Grammar:

Copy code The code is as follows:

buffer.toJSON()

Receive parameters:

None

Example:

Copy code The code is as follows:

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);
//

Source code:

Copy code The code is as follows:

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