[ANN] First release of ppx_deriving_jsont

I am happy to announce the first release of ppx_deriving_jsont.

As the name implies, it is a tool that automates some of the burden inherent to writing Jsont descriptions. Jsont is a fairly novel library for declarative JSON data manipulation, created by @dbuenzli.

This ppx can manage most of the basic types, tuples, variants and records, but still lacks a lot of features and control. It is also quite bad at error reporting. The current roadmap, which highlights existing and missing features can be found in the project’s readme along with many examples. I focused on generating readable and reusable code, close to the cookbook illustrations.

Note that Jsont offers much finer control and many more features than what that you can achieve using this crude ppx. Please take some time to read about all of its intricacies:

When precision and good error handling are required, such as when interacting with external json sources, thoughtfully crafted descriptions should still be considered. But I do believe this ppx to be very useful for bootstrapping, prototyping or when both the encoders and decoders are entirely under the app’s control. I wrote it to that intent and already use it in several personal projects. I hope you will find it useful too! (and not too non-sensical :slightly_smiling_face:)

Example:

type t = {
  name : string; [@jsont.doc "Doc for t.name"]
  maybe_parent : t option; [@option]
  ids : string list; [@default []] [@omit fun l -> l = []]
}
[@@kind "T2"] [@@doc "Doc for t"] [@@deriving jsont]

Generates the following description:

let jsont =
  let rec jsont =
    lazy
      (let make name maybe_parent ids = { name; maybe_parent; ids } in
       Jsont.Object.map ~doc:"Doc for t" ~kind:"T2" make
       |> Jsont.Object.mem "name" ~doc:"Doc for t.name" Jsont.string
            ~enc:(fun t -> t.name)
       |> Jsont.Object.mem "maybe_parent"
            (Jsont.option (Jsont.rec' jsont))
            ~enc:(fun t -> t.maybe_parent)
            ~dec_absent:None ~enc_omit:Option.is_none
       |> Jsont.Object.mem "ids" (Jsont.list Jsont.string)
            ~enc:(fun t -> t.ids)
            ~dec_absent:[]
            ~enc_omit:(fun l -> l = [])
       |> Jsont.Object.finish)
  in
  Lazy.force jsont
8 Likes

Cool! In related news, I’ve also just released another use of Jsont in the form of a JSONFeed implementation; lib/jsonfeed.mli at main · @anil.recoil.org/ocaml-jsonfeed · tangled

Wish I’d seen ppx_deriving_jsont before doing this! Although it is quite nice being ppx-free with the Jsont combinators, for ease of dependency management…

1 Like