🗜️ An example app that uses protobufs for serializing/deserializing JSON objects
package bookpackage;
syntax = "proto3";
message Book {
string title = 1;
optional string subtitle = 2;
string author = 3;
optional string published = 4;
optional string publisher = 5;
int32 pages = 6;
string description = 7;
optional string website = 8;
}
- The
.proto
file is the file that contains the schema for the JSON object. It is used to generate the code for the language you are using. - The
package
is the name of the package that the code will be generated in. - The
syntax
is the version of the protobuf syntax that you are using. - The
message
is the name of the JSON object.
decode
- decodes a buffer into a JSON objectencode
- encodes a JSON object into a bufferverify
- verifies that a JSON object is valid
Protobuf is a serialization format that is language agnostic. It is used to serialize/deserialize JSON objects. It is used in many Google products such as Google Maps, Google Drive, etc.
- It is language agnostic
- It is fast
- It is small
- It is easy to use
- Install the protobuf compiler (depending on the language you are using - I used
protobufjs
for Node.js) - Create a .proto file
- Generate the code for the language you are using
- Use the generated code to serialize/deserialze JSON objects
- Clone this repo
- Run
npm install
- Run
node .
- Soon:tm: (I'm still learning Docker)
- I used Node v19.3.0 and there were changes to the import syntax recently so you might have to use Node v18