Object Serialization in Node.js: JSON, Buffer and Protobuf

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Object Serialization in JavaScript (Node.js): from JSON to Binary Protocols



Serialization is the process of converting complex data structures (objects, arrays, Map, Set) into a format that can be saved to a file, transmitted over a network, or stored in a database. In Node.js, this task is especially critical, as server applications often work with large volumes of data, microservices, and WebSocket connections. In this article, we will cover the main tools and libraries for object serialization in the Node.js environment.



1. Built-in JSON: The De Facto Standard

The JSON module is available in Node.js without installing additional packages. It supports the JSON.stringify() and JSON.parse() methods.



const user = {  name: 'Anna',  age: 28,  skills: ['JavaScript', 'Node.js'],  active: true};

// Serializationconst jsonString = JSON.stringify(user);console.log(jsonString);// {"name":"Anna","age":28,"skills":["JavaScript","Node.js"],"active":true}

// Deserializationconst parsedUser = JSON.parse(jsonString);console.log(parsedUser.name); // Anna


Pros:

  • Human-readable format
  • Built-in support in all environments
  • Ease of debugging


Cons:

  • Does not support undefined, BigInt, Date (converts to string), Map, Set
  • Slower than binary formats
  • Redundant size due to syntactic elements (quotes, commas)


2. Buffer and Binary Serialization in Node.js

For high-performance applications (games, financial data, IoT), binary formats are used. In Node.js, the Buffer module is available for this purpose.



const buf = Buffer.alloc(8);buf.writeUInt32BE(123456, 0); // write 4-byte unsigned integerbuf.writeUInt32BE(789012, 4); // second number

console.log(buf); // <Buffer 00 01 e2 40 00 0c 08 14>

// Readingconst a = buf.readUInt32BE(0); // 123456const b = buf.readUInt32BE(4); // 789012


For complex object serialization in binary format, it is better to use specialized libraries.



3. Protocol Buffers (protobuf) — Efficient Binary Serialization

The protobuf.js library allows serializing objects according to a schema described in .proto files. This is a standard used in gRPC and many microservice architectures.



Installation:

npm install protobufjs


Usage example:

const protobuf = require('protobufjs');

protobuf.load('user.proto').then(root => { const User = root.lookupType('userpackage.User');

const payload = { id: 1, name: 'Ivan', email: 'ivan@example.com' }; // Serialization const errMsg = User.verify(payload); if (errMsg) throw Error(errMsg); const buffer = User.encode(payload).finish(); console.log('Binary buffer:', buffer);

// Deserialization const decoded = User.decode(buffer); console.log('Decoded:', decoded);});


File user.proto:

syntax = "proto3";package userpackage;message User {  int32 id = 1;  string name = 2;  string email = 3;}


Pros:

  • Maximum performance
  • Compact size (30-50% smaller than JSON)
  • Strong typing and validation
  • Cross-platform


Cons:

  • Requires schema description (.proto)
  • More difficult debugging (binary data)
  • Need to synchronize schemas between services


4. MessagePack — Binary JSON

Recommendations