I’m happy to announce the release of ppx_yojson_conv.
Ppx_yojson_conv is a syntax extension that generates code for converting OCaml types to and from Yojson.Safe.t, as defined in the yojson library. It has been written by Xueyuan Zhao, as part of his JaneStreet internship (summer 2019).
Ppx_yojson_conv is an alternative to ppx_deriving_yojson based on ppxlib that leverages all the experience acquired with ppx_sexp_conv over the years.
It tries to follow the serialization and parsing semantic of ppx_deriving_yojson to ease the transition between the two derivers.
Some notable differences that motivated the creation of ppx_yojson_conv:
It has better support for various ocaml construct (nonrec, polymorphic variants, non-regular types, …)
It doesn’t rely on polymorphic comparison for code generated for “default” attributes.
ppxlib will make sure all attributes have been consumed (allowing to spot typos)
It generates different function names: t_of_yojson/yojson_of_t instead of of_yojson/to_yojson
It supports types that reuse basic type names (string, int, unit, …)
ppx_yojson_conv was eavily based on ppx_sexp_conv. In particular we kept the same naming scheme.
I’m don’t for sure the motivation for the naming in ppx_sexp_conv but here are some possible reasons.
Don’t treat type [t]s specially. One can argue that it would confusing to use different naming scheme depending of the type name. (of_sexp vs u_of_sexp).
using the *_of_* naming only (not using *_to_*) makes the naming scheme fully symmetric and easier to remember. No need to figure out what to use for yojson_to_int / int_of_yojson and int_to_yojson / yojson_of_int.
Thank you for releasing it! We are trying to use ppx_yojson_conv to build the merlin-lsp for OCaml 4.02 - 4.08.
The idea behind the approach is using the [@@deriving_inline yojson]. This permits us to get the code we could commit and use to build with OCaml 4.02/4.03. The last piece which remains unresolved is the dependency on the ppx_yojson_conv.runtime-lib. Since it’s packaged up together with ppx_yojson_conv we can’t build it with 4.02/4.03. Currently we have just vendor’ed it directly in the repo, but that’s obviously not the best possible solution. Do you have any suggestion how we could do that properly?