[ANN] Ppx_yojson_conv: deriving plugin to generate Yojson conversion functions

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, …)

Documentation is available at https://github.com/janestreet/ppx_yojson_conv

The package should be available in opam shortly.


I think it fits this goal: https://github.com/ocaml-ppx/ppx_deriving_yojson/issues/103

Note, there is also a JSON driver for https://github.com/andersfugmann/ppx_protocol_conv

Really glad to see this work. Quick question, what is the motivation behind the function naming?

t_of_yojson/yojson_of_t instead of of_yojson/to_yojson

I always wondered about that when using sexplib too.


I’m curious about the motivation for this? Seems non-standard?

EDIT: Oops, @keleshev was faster.

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?

1 Like

@trefis has worked on splitting the ppx_yojson_conv runtime to a different library. See https://github.com/janestreet/ppx_yojson_conv_lib