• Node.js Video Tutorials

NodeJS - v8.deserialize() 方法



NodeJS v8.deserialize() 方法用于将缓冲区值(由 serialize() 方法返回)反序列化为 JavaScript 值。

此方法使用默认的反序列化器和默认选项从缓冲区读取 JavaScript 值。

语法

以下是NodeJS v8.deserialize() 方法的语法:

v8.deserialize(buffer)

参数

此方法只接受一个参数。参数描述如下。

  • buffer − 传递给参数的值由 serialize() 方法返回。它可以是 Buffer/TypedArray/DataView,并将使用默认的反序列化器反序列化为 JavaScript 值。

返回值

此方法在反序列化缓冲区数据后返回一个 JavaScript 值。

示例

在下面的示例中,我们尝试使用 NodeJS v8.deserialize() 方法将缓冲区值反序列化为 JavaScript 值。

const v8 = require("v8");
let value = "Tutorialspoint";
console.log("The value: " + value);
console.log("Value after serializing: ");
let serialized = v8.serialize(value);
console.log(v8.serialize(value));
console.log("Value after deserializing: ");
console.log(v8.deserialize(serialized));

输出

首先,我们使用 NodeJS serialize() 方法将字符串数据类型序列化为缓冲区值。然后,我们使用 NodeJS deserialize() 方法反序列化返回的 Buffer 值。

The value: Tutorialspoint
Value after serializing: 
<Buffer ff 0d 22 0e 54 75 74 6f 72 69 61 6c 73 70 6f 69 6e 74>
Value after deserializing: 
Tutorialspoint

示例

在下面的示例中,我们尝试使用 NodeJS v8.deserialize() 方法将不同的缓冲区值反序列化为 JavaScript 值。

const v8 = require("v8");
let value1 = 354423.65462
let value2 = {firstName:"Gautham", lastName:"Menon"};
let value3 = true;

console.log("The value1: " + value1);
console.log("Value after serializing: ");
let serialized = v8.serialize(value1);
console.log(v8.serialize(value1));

console.log("Value after deserializing: ");
console.log(v8.deserialize(serialized));

console.log("-----------------------------------------------");

console.log("The value2: " + JSON.stringify(value2));
console.log("Value after serializing: ");
let serialized1 = v8.serialize(value2);
console.log(v8.serialize(value2));

console.log("Value after deserializing: ");
console.log(v8.deserialize(serialized1));

console.log("-----------------------------------------------");
console.log("The value3: " + value3);

console.log("Value after serializing: ");
let serialized2 = v8.serialize(value3);
console.log(v8.serialize(value3));

console.log("Value after deserializing: ");
console.log(v8.deserialize(serialized2));

输出

以下是上述程序的输出:

The value1: 354423.65462
Value after serializing: 
<Buffer ff 0d 4e 8d b4 54 9e de a1 15 41>
Value after deserializing: 
354423.65462
-----------------------------------------------
The value2: {"firstName":"Gautham","lastName":"Menon"}
Value after serializing: 
<Buffer ff 0d 6f 22 09 66 69 72 73 74 4e 61 6d 65 22 07 47 61 75 74 68 61 6d 22 08 6c 61 73 74 4e 61 6d 65 22 05 4d 65 6e 6f 6e 7b 02>
Value after deserializing: 
{ firstName: 'Gautham', lastName: 'Menon' }
-----------------------------------------------
The value3: true
Value after serializing: 
<Buffer ff 0d 54>
Value after deserializing: 
True
nodejs_v8_module.htm
广告