How to convert the buffer in Nodejs to JSON format and UTF-8 string? The following article will introduce to you the method of converting buffer and JSON formats in Nodejs, as well as the method of converting Buffer to UTF-8 string.
Node.js is different from browser-based JavaScript because Node had a way to handle binary data even before the ES6 draft proposed ArrayBuffer
. In Node, the Buffer
(buffer) class is the primary data structure used by most I/O operations. It is raw binary data allocated outside the V8 heap and cannot be resized once allocated. [Recommended learning: "nodejs Tutorial"]
Before Node v6.0, to create a new buffer, we only need to use the new
keyword to call the constructor Function:
let newBuff = new Buffer('I Love You')
After v6.0, to create a new buffer instance:
let newBuff = Buffer.from('I Love You')
new Buffer()
The constructor has been deprecated and replaced by a separate Buffer.from()
, Buffer.alloc()
and Buffer.allocUnsafe()
method replacement.
For more information, you can readOfficial Document.
Buffers can be converted to JSON.
let bufferOne = Buffer.from('All work and no play makes Jack a dull boy') console.log(bufferOne) // <Buffer 41 6c 6c 20 77 6f 72 6b 20 61 6e 64 20 6e 6f 20 70 6c 61 79 20 6d 61 6b 65 73 20 4a 61 63 6b 20 61 20 64 75 6c 6c 20 62 6f 79> let json = JSON.stringify(bufferOne, null, 2) console.log(json) /* { "type": "Buffer", "data": [ 65, 108, 108, 32, 119, 111, 114, 107, 32, 97, 110, 100, 32, 110, 111, 32, 112, 108, 97, 121, 32, 109, 97, 107, 101, 115, 32, 74, 97, 99, 107, 32, 97, 32, 100, 117, 108, 108, 32, 98, 111, 121 ] } */
JSON Specifies that the type of object to be converted is Buffer
and its data.
let bufferOriginal = Buffer.from(JSON.parse(json).data) console.log(bufferOriginal) // <Buffer 41 6c 6c 20 77 6f 72 6b 20 61 6e 64 20 6e 6f 20 70 6c 61 79 20 6d 61 6b 65 73 20 4a 61 63 6b 20 61 20 64 75 6c 6c 20 62 6f 79>
console.log(bufferOriginal.toString('utf8')) // All work and no play makes Jack a dull boy
. toString()
is not the only way to convert a buffer to a string. Additionally, by default, it is converted to a utf-8 format string.
Another way to convert a buffer to a string is to use the StringDecoder
core module in the Node.js API.
string_decoder
The module provides a way to decode a Buffer
object into a string (to preserve encoded multibyte UTF-8 and UTF -16 character mode) API. An alternative to the above example is as follows:
const { StringDecoder } = require('string_decoder') const decoder = new StringDecoder('utf8') let bufferOriginal = Buffer.from(JSON.parse(json).data) console.log(decoder.write(bufferOriginal)) // All work and no play makes Jack a dull boy console.log(decoder.end(bufferOriginal)) // All work and no play makes Jack a dull boy
When the Buffer
instance is written to the StringDecoder
instance, the internal buffer is used to ensure that the decoded string is not Contains any incomplete multibyte characters. These are kept in the buffer until the next call to stringDecoder.write()
or the next call to stringDecoder.end()
.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of How to convert buffer in Nodejs to JSON format and UTF-8 string?. For more information, please follow other related articles on the PHP Chinese website!