Node.js – v8.deserialize() 方法
该V8。deserialize()方法提供了一种将序列化缓冲区反序列化回原始文本的方法。此方法使用带有默认选项的默认解串器从缓冲区读取原始JS值。
语法
v8.deserialize(buffer)
参数
buffer- 将被反序列化的输入缓冲区。
该函数在对输入缓冲区进行反序列化后返回原始文本输出。
示例1
创建一个文件“deserialize.js”并复制以下代码片段。创建文件后,使用命令“nodedeserialize.js”运行此代码。
//v8.deserialize()演示示例 //导入v8模块 const v8 = require('v8'); //初始化数据变量 var data = "Welcome tonhooo.com!" //序列化值 var serializedValue = v8.serialize(data); //打印序列化和反序列化的值 console.log('Serialized data : ', serializedValue) console.log('Data after de-serializing', v8.deserialize(serializedValue))输出结果
C:\home\node>> node deserialize.js Serialized data :Data after de-serializing Welcome tonhooo.com!
示例2
让我们再看一个例子
//v8.deserialize()演示示例 //导入v8模块 const v8 = require('v8'); //初始化数据变量 var data = "Welcome tonhooo.com!" //序列化值 var serializedValue = v8.serialize(data); //打印序列化和反序列化的值 console.log('Serialized data : ', serializedValue) console.log('Data after de-serializing', v8.deserialize(serializedValue)) var number = v8.serialize(12345); console.log('Serializing numbers', v8.serialize(number)) console.log('Data after de-serializing', v8.deserialize(number)) var decimal = v8.serialize(12345.543); console.log('Serializing decimal', v8.serialize(decimal)) console.log('Data after de-serializing', v8.deserialize(decimal))输出结果
C:\home\node>> node deserialize.js Serialized data :Data after de-serializing Welcome tonhooo.com! Serializing numbers Data after de-serializing 12345 Serializing decimal Data after de-serializing 12345.543