• Node.js Video Tutorials

NodeJS v8.deserializer.readValue() Method



The NodeJS v8.deserializer.readValue() method of class v8.deserializer is used for deserializing the JavaScript value that is written in the internal buffer (using the writeValue() method).

Syntax

Following is the syntax of the NodeJS readValue() method

v8.deserializer.readValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the JavaScript value from the internal buffer after deserializing it.

Example

In the following example, firstly, we are trying to write a JavaScript value to the internal buffer using the v8.serializer.writeValue() method. Then we are trying to read and return the value present in the internal buffer using the v8.deserializer.readValue() method.

const v8 = require('v8');

// Defining the serializer object
const serializer = new v8.Serializer();

// Writing some value to internal buffer
console.log(serializer.writeValue('TutorialsPoint'));

// Defining the deserializer object
const deserializer = new v8.Deserializer(serializer.releaseBuffer());

// Reading the headers from internal buffer
console.log(deserializer.readValue());

Output

true
TutorialsPoint

Example

In this example, we are trying to write a header instead of value to the internal buffer. So, the readValue() method will throw an error.

const v8 = require('v8');

// Defining the serializer object
const serializer = new v8.Serializer();

// Writing some value to internal buffer
serializer.writeHeader('TutorialsPoint');

// Defining the deserializer object
const deserializer = new v8.Deserializer(serializer.releaseBuffer());

// Reading the headers from internal buffer
console.log(deserializer.readValue());

Error

Following is the output of the above code −

/home/cg/root/63b6984e13ac0/main.js:13
console.log(deserializer.readValue());
                         ^

Error: Unable to deserialize cloned data.
    at Object.<anonymous> (/home/cg/root/63b6984e13ac0/main.js:13:26)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_v8_module.htm
Advertisements