This article mainly introduces node. buffer in js. description of the toJSON method. This article introduces the buffer. toJSON method description, syntax, receiving parameters, use instances and implementation source code. For more information, see
Method description:
Converts a buffer object to json format.
Syntax:
The Code is as follows:
Buffer. toJSON ()
Receiving parameters:
None
Example:
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:
The Code is as follows:
Buffer. prototype. toJSON = function (){
Return {
Type: 'buffer ',
Data: Array. prototype. slice. call (this, 0)
};
};