proto3-json-serializer

Support for proto3 JSON serialiazation/deserialization for protobuf.js

Apache-2.0 27 个版本
安装
npm install proto3-json-serializer
yarn add proto3-json-serializer
pnpm add proto3-json-serializer
bun add proto3-json-serializer
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.

版本列表
4.0.0-experimental 2025-03-25
3.0.5 2026-06-04
3.0.4 2025-10-30
3.0.3 2025-10-13
3.0.2 2025-08-12
3.0.1 2025-07-01
3.0.0 2025-02-18
2.0.2 2024-05-22
2.0.2-experimental 2025-02-13
2.0.1 2024-01-16
2.0.0 2023-08-07
1.1.1 2023-04-25
1.1.0 2022-08-26
1.0.3 2022-07-21
1.0.2 2022-06-15
1.0.1 2022-06-03
1.0.0 2022-05-12
0.1.9 2022-05-12
0.1.8 2022-01-21
0.1.7 2022-01-14
0.1.6 2021-11-15
0.1.5 2021-10-26
0.1.4 2021-09-20
0.1.3 2021-08-18
0.1.2 2021-08-17
0.1.1 2021-08-04
0.1.0 2021-08-03