Home > Web Front-end > JS Tutorial > body text

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

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

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.

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 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')
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 separate Buffer.from(), Buffer.alloc() and Buffer.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)

// <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
  ]
}
*/
Copy after login

JSON Specifies that the type of object to be converted is Buffer and its data.

Convert JSON to Buffer

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>
Copy after login

Convert Buffer to UTF-8 string

console.log(bufferOriginal.toString(&#39;utf8&#39;)) // 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 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(&#39;string_decoder&#39;)
const decoder = new StringDecoder(&#39;utf8&#39;)

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 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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template