No description
Find a file
Oleg `Kanedias` Chernovskiy ab7469155a
Start prototyping the tests
2026-01-02 13:20:32 +02:00
src Split into cli and converter 2026-01-02 13:20:12 +02:00
testdata Start prototyping the tests 2026-01-02 13:20:32 +02:00
tests Start prototyping the tests 2026-01-02 13:20:32 +02:00
.gitignore Initial commit 2025-12-27 21:55:45 +02:00
.gitlab-ci.yml Add basic CI 2025-12-28 00:05:13 +02:00
Cargo.lock Cleanup and proper readme 2025-12-27 22:56:38 +02:00
Cargo.toml Cleanup and proper readme 2025-12-27 22:56:38 +02:00
LICENSE Add LICENSE file 2025-12-27 23:01:52 +02:00
README.md More readme content 2025-12-27 23:01:33 +02:00

JSON to binary protobuf converter

This small utility converts an arbitrary JSON object into a binary protobuf message. It does not generate any code, nor does it require parsed files to already be present.

The only thing it needs is a .proto file with a requested message type inside. After the JSON object is converted, it outputs it to the stdout.

Rationale

This executable was written mostly to complement the grpcurl utility, as quite many protobuf schemas require opaque binary payloads, which are their own protobuf messages inside. grpcurl lacks the ability to convert those efficiently, so json2pb was needed to properly handle such cases.

Usage

Usage: json2pb [OPTIONS] --proto-file <FILE> --message-type <TYPE>

Options:
  -i, --input <FILE>
          input JSON file for parsing

          [default: -]

  -o, --output <FILE>
          output binary file for writing

          [default: -]

  -I, --import-path <DIRS>
          import paths for dependant proto files

  -p, --proto-file <FILE>
          proto file that contains message type definitions

  -m, --message-type <TYPE>
          message type to generate

  -v, --verbose...
          Increase logging verbosity

  -q, --quiet...
          Decrease logging verbosity

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Example: echo '{"your-json": "goes-here"}' | json2pb -I protos/ -p protos/test.proto -m TestMessage

Known issues

This utility needs an extensive test set with various protobuf messages available. I've written it to help myself with .proto files at my work, but those are under NDA. If you have a particularly peculiar protobuf schema and can share it, create an issue in this repo.

Also, I haven't tested OneOf capabilities at all. They might work, but they also may fail spectacularly.

Licensing

Copyright (C) 2025  Oleg `Kanedias` Chernovskiy

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.