How to convert buffer in Nodejs to JSON format and UTF-8 string?

青灯夜游
Release: 2021-08-26 10:25:07
forward
5595 people have browsed it

How to convert the buffer inNodejsto 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.

How to convert buffer in Nodejs to JSON format and 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 proposedArrayBuffer. In Node, theBuffer(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 thenewkeyword to call the constructor Function:

let newBuff = new Buffer('I Love You')
Copy after login

After v6.0, to create a new buffer instance:

let newBuff = Buffer.from('I Love You')
Copy after login

new Buffer()The constructor has been deprecated and replaced by a separateBuffer.from(),Buffer.alloc()andBuffer.allocUnsafe()method replacement.

For more information, you can readOfficial Document.

Convert Buffer to JSON

Buffers can be converted to JSON.

let bufferOne = Buffer.from('All work and no play makes Jack a dull boy') console.log(bufferOne) //  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 ] } */
Copy after login

JSON Specifies that the type of object to be converted isBufferand its data.

Convert JSON to Buffer

let bufferOriginal = Buffer.from(JSON.parse(json).data) console.log(bufferOriginal) // 
Copy after login

Convert Buffer to UTF-8 string

console.log(bufferOriginal.toString('utf8')) // All work and no play makes Jack a dull boy
Copy after login

. 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 theStringDecodercore module in the Node.js API.

string_decoderThe module provides a way to decode aBufferobject 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
Copy after login

When theBufferinstance is written to theStringDecoderinstance, 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 tostringDecoder.write()or the next call tostringDecoder.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!

Related labels:
source:juejin.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!