Protobuf generator, encoder and decoder.
  • v2.2.2 - November 3, 2020
  • v2.1.2 - May 11, 2018
  • v2.1.1 - August 5, 2017
  • v2.1.0 - August 3, 2017
  • v2.0.2 - August 24, 2016

protobuf Build Status Dependency Status devDependency Status

Crystal shard to decode, encode and generate protobuf messages.


Add this to your application's shard.yml:

    github: jeromegn/

Install protobuf

macOS: brew install protobuf

Ubuntu sudo apt-get install -y protobuf-compiler


This library does not follow semver conventions for versioning. It started off at v2.0.0 because it supports protobuf v2. From there, it'll increment versions based on semver, except for breaking changes, where I will not increment the major version.


Decoding and encoding messages

to_protobuf(io : IO) and from_protobuf(io : IO) are available on enums and anything that includes Protobuf::Message and defines a contract


require "protobuf"

enum Foo

struct MyMessage
  include Protobuf::Message
  contract do
    # some required properties
    required :prop_name, :int32, 1, default: 123
    required :prop2, Foo, 2

    # optional properties
    optional :optional_prop_name, :string, 3

    # repeated fields
    repeated :my_array, :int32, 4 # produces a property of type Array(Int32)?

  # write your methods like you normally would here, if you like.

proto_io ="path/to/encoded/protobuf") # get your IO in some way

msg = MyMessage.from_protobuf(proto_io) # returns a an instance of MyMessage
                                  # from a valid protobuf encoded message

msg.to_protobuf # return a IO::Memory filled with the encoded message

some_io =
msg.to_protobuf(some_io) # fills up the provided IO with the encoded message

Field types

All field types supported by the protobuf protocol v2 are available as symbols or the name of a Crysta; struct or class.


Using the contract block creates an initializer with all the properties defined in it. It also creates an initializer which can consume a Protobuf::Buffer (used by from_protobuf method), not unlike JSON::PullParser.

Generating Crystal protobuf messages

Protobuf provides the protoc executable to encode, decode and generate language-specific protobuf messages via plugins.

1. Install the protoc plugin

brew install jeromegn/tap/protoc-gen-crystal
crystal build bin/ -o ~/bin/protoc-gen-crystal

2. Generate files

protoc -I <.protos_basepath> --crystal_out <path_to_folder_for_protobufs> <path_to_{.proto,*.protos}>

Generator options

The generator is configurable via environment variables:

  • PROTOBUF_NS - If your want to namespace everything under a module (default: ""). Please write with a CamelCase format (ie: "MesosMessage" would produce module MesosMessage)
  • STRIP_FROM_PACKAGE - Protobuf has package namespaces and sometimes messages reference other namespaces (ie: mesos.v1.scheduler.Call), but you want those to be namespaced in a Crystal-like fashion (ie: Scheduler::Call), then you can specify a string to strip from the packages for each file (ie: STRIP_FROM_PACKAGE=mesos.v1) and the rest will be CamelCased

Known Limitations

  • Does not support non-UTF8 strings...


To generate encoded protobufs, you can create a .proto and a "raw" data protobuf file and use protoc to encode the data.

For example:

cat spec/fixtures/ | protoc -I spec/fixtures --encode=Test spec/fixtures/test.proto > spec/fixtures/


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


  • jeromegn Jerome Gravel-Niquet - creator, maintainer
github statistic
  • 75
  • 7
  • 17
  • 9
  • 2 months ago
  • August 21, 2016

MIT License

Synced at

Fri, 15 Jan 2021 11:54:58 GMT