issuu / ocaml-protoc-plugin

ocaml-protoc-plugin
https://issuu.github.io/ocaml-protoc-plugin/
Other
48 stars 19 forks source link

JSON encoding #29

Closed vphantom closed 7 months ago

vphantom commented 2 years ago

Hi! Thank you for open sourcing this compiler and supporting library. It looks like a great alternative to ocaml-protoc with a few interesting design choices. I just realized there's no JSON encoding/decoding support.

Is this in your roadmap at all?

quernd commented 2 years ago

We wondered the same very recently, and it turns out that you can get a long way with PPX that will automatically generate JSON encoders and decoders for you, such as ppx_deriving_yojson, ppx_yojson_conv or ppx_protocol_conv.

The only types for which you have to supply the encoder/decoder functions yourself are Extensions.t and Field.t. In our fork there is an example that works with both ppx_deriving_yojson and ppx_yojson_conv for serialization to JSON. If you need to go from JSON to Protobuf (we haven't had that need yet) you will need to write that bit yourself. The different PPX require slightly different signatures but otherwise they are similar. You could also make your life very easy and just slap [@@deriving yojson] on it in extensions.ml and field.ml, but that would make the PPX a dependency of ocaml-protoc-plugin.

Lupus commented 1 year ago

But there are some nuances, 64bit integers are encoded as strings, along with some special float values.

image

Also field names are adapted to OCaml Snake_case notation, while in protobuf fields are named in CamelCase, and json encoding expects them to follow original CamelCase names. I was thinking about using this plugin with twirp, which is a lightweight RPC built on protobuf, which also natively supports JSON encoding in case you need to curl your API or integrate with some language where protobufs are not the best choice (like JavaScript running in browser).

Would still be awesome to have native support for JSON encoding in this otherwise great plugin!

andersfugmann commented 7 months ago

This feature is planned for version 5.1 (with 5.0 being eminent). See https://github.com/andersfugmann/ocaml-protoc-plugin/issues/4