HomeToolsAbout a20k

Protobuf

What is it

Source

Method developed by Google for serializing structured data

  • often used for efficient and language-agnostic communication between different systems or for data storage
  • method of transferring structured data across the wire and storing on the disk
// Has a language-agnostic binary format syntax = "proto3" message Person { string name = 1; int32 id = 2; string email = 3; }

protoc

protoc is a compiler code provided by Google that converts any protobuff to target language compatible format

Anatomy

Schema definition is written in .proto files

message employee { int32 id = 1; string name = 2; float salary = 3; }
  • Defines a field within the message.
  • It specifies that the field with data type, and it's named.
  • The number (= 1) is the field's unique identifier.
  • In Protocol Buffers, fields are numbered for identification and versioning.

Instantiation

const employee = new Schema.Employee(); employee.setId(1002); employee.setName("Sam"); employee.setSalary(9000); /* JSON equivalent */ const employees = []; employees.push({ "name": "Sam", "salary": 9000, "id": 1002, });
© VincentVanKoh