- Rust 100%
| src | ||
| testdata | ||
| tests | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| Cargo.lock | ||
| Cargo.toml | ||
| LICENSE | ||
| README.md | ||
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.