Hacker News new | ask | show | jobs
by this_was_posted 20 days ago
Slightly off topic, but is anyone aware of a compile free method to convert protobuf messages to json representation based on a provided .proto file (and the other way around)? All protobuf implementations seem to require a compilation step, which makes it hard to support en-/decoding untrusted content using user provided schemas
2 comments

The buf CLI can do this.

Here's how it looks to convert from encoded protobuf to json (protojson).

  buf convert schema.proto \
    --type YourMessageName \
    --from payload.binpb \
    --to output.json
And just invert the arguments to convert back.

  buf convert schema.proto \
    --type YourMessageName \
    --from data.json \
    --to encoded.binpb
There is the --encode (and --decode) options for the `protoc` executable, e.g. `cat myobject.json | protoc --encode MyObject protofile.proto > myobject.bin`