All Projects → mransan → Ocaml Protoc

mransan / Ocaml Protoc

Licence: mit
A Protobuf Compiler for OCaml

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to Ocaml Protoc

Protobuf Convert
Macros for convenient serialization of Rust data structures into/from Protocol Buffers 3
Stars: ✭ 22 (-82.95%)
Mutual labels:  protobuf, serialization, protocol
Fastbinaryencoding
Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift
Stars: ✭ 421 (+226.36%)
Mutual labels:  serialization, protocol
Ceras
Universal binary serializer for a wide variety of scenarios https://discord.gg/FGaCX4c
Stars: ✭ 374 (+189.92%)
Mutual labels:  serialization, protocol
Cpp Serializers
Benchmark comparing various data serialization libraries (thrift, protobuf etc.) for C++
Stars: ✭ 533 (+313.18%)
Mutual labels:  protobuf, serialization
Shineframe
高性能超轻量级C++开发库及服务器编程框架
Stars: ✭ 274 (+112.4%)
Mutual labels:  protobuf, serialization
Kotlinx.serialization
Kotlin multiplatform / multi-format serialization
Stars: ✭ 3,550 (+2651.94%)
Mutual labels:  protobuf, serialization
Protobuf
A pure Elixir implementation of Google Protobuf
Stars: ✭ 442 (+242.64%)
Mutual labels:  protobuf, serialization
tinypacks
A data serialization format for constrained environments like 8-bit and 16-bit microcontrollers.
Stars: ✭ 30 (-76.74%)
Mutual labels:  serialization, protocol
Coq Serapi
Coq Protocol Playground with Se(xp)rialization of Internal Structures.
Stars: ✭ 87 (-32.56%)
Mutual labels:  serialization, protocol
Protobuf Nim
Protobuf implementation in pure Nim that leverages the power of the macro system to not depend on any external tools
Stars: ✭ 90 (-30.23%)
Mutual labels:  protobuf, serialization
Protobuf
Protocol Buffers - Google's data interchange format
Stars: ✭ 52,305 (+40446.51%)
Mutual labels:  protobuf, serialization
ocaml-pb-plugin
A protoc plugin for generating OCaml code from protobuf (.proto) files.
Stars: ✭ 18 (-86.05%)
Mutual labels:  serialization, protobuf
nimpb
Protocol Buffers for Nim
Stars: ✭ 29 (-77.52%)
Mutual labels:  serialization, protobuf
Flatbuffers
FlatBuffers: Memory Efficient Serialization Library
Stars: ✭ 17,180 (+13217.83%)
Mutual labels:  protobuf, serialization
CppSerialization
Performance comparison of the most popular C++ serialization protocols such as Cap'n'Proto, FastBinaryEncoding, Flatbuffers, Protobuf, JSON
Stars: ✭ 89 (-31.01%)
Mutual labels:  serialization, protobuf
Flatcc
FlatBuffers Compiler and Library in C for C
Stars: ✭ 434 (+236.43%)
Mutual labels:  serialization, protocol
protobuf-d
Protocol Buffers Compiler Plugin and Support Library for D
Stars: ✭ 32 (-75.19%)
Mutual labels:  serialization, protobuf
javascript-serialization-benchmark
Comparison and benchmark of JavaScript serialization libraries (Protocol Buffer, Avro, BSON, etc.)
Stars: ✭ 54 (-58.14%)
Mutual labels:  serialization, protobuf
Rust Protobuf
Rust implementation of Google protocol buffers
Stars: ✭ 1,797 (+1293.02%)
Mutual labels:  protobuf, serialization
Luapbintf
Binding Protobuf 3 to Lua 5.3
Stars: ✭ 122 (-5.43%)
Mutual labels:  protobuf, protocol

ocaml-protoc

🐪 A protobuf compiler for OCaml 🐪.

Introduction

ocaml-protoc compiles protobuf message files into OCaml types along with serialization functions for a variety of encodings.

ocaml-protoc supports both proto syntax 2 and 3 as well as binary and JSON encodings.

ocaml-protoc supports JavaScript object encoding through the BuckleScript compiler. See here for complete example.

A simple example

This example generates the binary encoding, if you are more interested in a JavaScript example, go here

  • Write in example.proto
message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
  repeated string phone = 4;
}
  • Run:
❯ ocaml-protoc -binary -ml_out ./ example.proto
.. Generating example_types.mli
.. Generating example_types.ml
.. Generating example_pb.mli
.. Generating example_pb.ml
  • example_types.mli:
(** example.proto Generated Types *)

(** {2 Types} *)

type person = {
  name : string;
  id : int32;
  email : string;
  phone : string list;
}

(** {2 Default values} *)

val default_person : 
  ?name:string ->
  ?id:int32 ->
  ?email:string ->
  ?phone:string list ->
  unit ->
  person
(** [default_person ()] is the default value for type [person] *)
  • example_pb.mli:
(** {2 Protobuf Encoding} *)

val encode_person : Example_types.person -> Pbrt.Encoder.t -> unit
(** [encode_person v encoder] encodes [v] with the given [encoder] *)

(** {2 Protobuf Decoding} *)

val decode_person : Pbrt.Decoder.t -> Example_types.person
(** [decode_person decoder] decodes a [person] value from [decoder] *)
  • in main.ml, write the following to encode a person value and save it to a file:
let () =

  (* Create OCaml value of generated type *) 
  let person = Example_types.({ 
    name = "John Doe"; 
    id = 1234l;
    email = Some "[email protected]"; 
    phone = ["123-456-7890"];
  }) in 
  
  (* Create a Protobuf encoder and encode value *)
  let encoder = Pbrt.Encoder.create () in 
  Example_pb.encode_person person encoder; 

  (* Output the protobuf message to a file *) 
  let oc = open_out "myfile" in 
  output_bytes oc (Pbrt.Encoder.to_bytes encoder);
  close_out oc
  • then in the same main.ml append the following to read from the same file:
let () = 
  (* Read bytes from the file *) 
  let bytes = 
    let ic = open_in "myfile" in 
    let len = in_channel_length ic in 
    let bytes = Bytes.create len in 
    really_input ic bytes 0 len; 
    close_in ic; 
    bytes 
  in 
  
  (* Decode the person and Pretty-print it *)
  Example_pb.decode_person (Pbrt.Decoder.of_bytes bytes)
  • ❗️ Int32 vs int

OCaml users will immediately point to the use of int32 type in the generated code which might not be the most convenient choice. One can modify this behavior using custom extensions.

Install & Build

Prerequesite

ocaml-protoc depends on :

  • the OCaml compiler distribution (byte code/native compiler and ocamlbuild).
  • ppx_deriving_protobuf for the generated code runtime.

Intall from OPAM

❯ opam install ocaml-protoc

Or from source

mkdir -p tmp/bin
❯ export PREFIX=`pwd`/tmp
❯ make install

Build your program

Using ocamlfind one can build the program above with the following:

❯ ocamlfind ocamlopt -linkpkg -package ocaml-protoc \
    -o example \
    example_types.mli example_types.ml \
    example_types.mli example_types.ml \
    main.ml

🏁 You can now run the example

❯ ./example

All Generated Files and Encodings:

file name Command line switch Description Runtime
<name>_types.{ml|mli} Type definition along with a constructor function to conveniently create values of that type
<name>_pb.{ml|mli} -binary Binary encodings ocaml-protoc
<name>_yojson.{ml|mli} -yojson JSON encoding using the widely popular yojson library ocaml-protoc-yojson
<name>_bs.{ml|mli} -bs BuckleScript encoding using the BuckleScript core binding to JS json library bs-ocaml-protoc-json
<name>_pp.{ml|mli} -pp pretty printing functions based on the Format module. ocaml-protoc

Protobuf <-> OCaml mapping

see here.

Compiler Internals

see here

Protobuf Extensions

see here

Benchmarking

see here

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].