Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

proto3-json-serializer

googleapis17.2mApache-2.02.0.2TypeScript support: included

Support for proto3 JSON serialiazation/deserialization for protobuf.js

protobufjs, protobuf.js, protobuf, proto3, json, serialization, deserialization

readme

proto3 JSON serializer for TypeScript / JavaScript

This library implements proto3 JSON serialization and deserialization for protobuf.js protobuf objects according to the spec.

Note that the spec requires special representation of some google.protobuf.* types (Value, Struct, Timestamp, Duration, etc.), so you cannot just use .toObject() since the result won't be understood by protobuf in other languages. Hence this module.

JavaScript:

const serializer = require('proto3-json-serializer');

TypeScript:

import * as serializer from 'proto3-json-serializer';

Serialization: protobuf.js object to proto3 JSON

const root = protobuf.loadSync('test.proto');
const Type = root.lookupType('test.Message');
const message = Type.fromObject({...});

const serialized = serializer.toProto3JSON(message);

Serialization works with any object created by calling .create(), .decode(), or .fromObject() for a loaded protobuf type. It relies on the $type field so it will not work with a static object.

Deserialization: proto3 JSON to protobuf.js object

To deserialize an object from proto3 JSON, we must know its type (as returned by root.lookupType('...')). Pass this type as the first parameter to .fromProto3JSON:

const root = protobuf.loadSync('test.proto');
const Type = root.lookupType('test.Message');
const json = {...};

const deserialized = serializer.fromProto3JSON(Type, json);

Complete example

const assert = require('assert');
const path = require('path');
const protobuf = require('protobufjs');
const serializer = require('proto3-json-serializer');

// We'll take sample protos from google-proto-files but the code will work with any protos
const protos = require('google-proto-files');

// Load some proto file
const rpcProtos = protos.getProtoPath('rpc');
const root = protobuf.loadSync([
    path.join(rpcProtos, 'status.proto'),
    path.join(rpcProtos, 'error_details.proto'),
]);
const Status = root.lookupType('google.rpc.Status');

// If you have a protobuf object that follows proto3 JSON syntax
// https://developers.google.com/protocol-buffers/docs/proto3#json
// (this is an example of google.rpc.Status message in JSON)
const json = {
    code: 3,
    message: 'Test error message',
    details: [
        {
            '@type': 'google.rpc.BadRequest',
            fieldViolations: [
                {
                    field: 'field',
                    description: 'must not be null',
                },
            ],
        },
    ],
};

// You can deserialize it into a protobuf.js object:
const deserialized = serializer.fromProto3JSON(Status, json);
console.log(deserialized);

// And serialize it back
const serialized = serializer.toProto3JSON(deserialized);
assert.deepStrictEqual(serialized, json);

Disclaimer

This is not an officially supported Google project.

changelog

Changelog

2.0.2 (2024-05-22)

Bug Fixes

  • Properly convert repeated int64 and maps of int64 (#96) (1ec05fb)

2.0.1 (2024-01-16)

Bug Fixes

  • deps: Update dependency google-proto-files to v4 (#82) (72623e0)
  • deps: Update protobufjs to 7.2.5 (a0f5c83)

2.0.0 (2023-08-07)

⚠ BREAKING CHANGES

  • require Node 14 (#72)

Miscellaneous Chores

1.1.1 (2023-04-25)

Bug Fixes

  • Repeated field can be null in JSON (#66) (f81d3ab)

1.1.0 (2022-08-26)

Features

  • option to serialize enum values as numbers (#60) (456b771)

Bug Fixes

1.0.3 (2022-07-10)

Bug Fixes

  • deps: update dependency protobufjs to v7 (#56) (038fea5)

1.0.2 (2022-06-15)

Bug Fixes

  • deps: update dependency google-proto-files to v3 (#53) (40fd527)

1.0.1 (2022-06-03)

Bug Fixes

  • deps: bump protobuf.js to ^6.11.3 (#46) (af8a14a)

1.0.0 (2022-05-12)

⚠ BREAKING CHANGES

  • make Node 12 minimum language version (#38)

Features

  • make Node 12 minimum language version (#38) (658d29e)

0.1.9 (2022-05-11)

Bug Fixes

0.1.8 (2022-01-21)

Bug Fixes

0.1.7 (2022-01-14)

Bug Fixes

  • keep nano second precision when maps between JSON and proto3 (#28) (eaa01ce)

0.1.6 (2021-11-15)

Bug Fixes

  • deps: protobufjs is a dependency for the types (#23) (06470c1)

0.1.5 (2021-10-26)

Bug Fixes

  • JSON accept special string for NaN, Infinity (#19) (01a345b)

0.1.4 (2021-09-20)

Bug Fixes

0.1.3 (2021-08-18)

Bug Fixes

  • do not fail for unknown enum values (#11) (ff9f0f1)

0.1.2 (2021-08-17)

Bug Fixes

  • use imported protobufjs in toproto3json.ts (#9) (f6c86c7)

0.1.1 (2021-08-04)

Bug Fixes

  • accept and return strings for int64 and uint64 (#7) (35689ec)

0.1.0 (2021-08-03)

⚠ BREAKING CHANGES

  • proto3 JSON serializer and deserializer (#2)

Features

  • proto3 JSON serializer and deserializer (#2) (96255a7)