A tactic I’ve been recently using with some success for this problem is to leverage the work I’ve already done for this sort of thing in Orsetto.
Orsetto has its own extensible runtime type identification system— given the lack of one in Stdlib
— which is available for use in both its JSON and CBOR parsing and formatting libraries. It is, for example possible to parse an opaque JSON value from an input stream and format that value as CBOR for output— without ever unpacking the structure of the value.
This is useful because it allows me to write one function for any given algebraic type that converts it to an opaque value that I can then serialize as either JSON text, a CBOR binary message, or even the textual CBOR diagnostic notation (an extension of JSON) as my needs arise.
One of the things I’m hoping to get around to writing, once the Ppx
element of the OCaml platform matures further, is a deriving layer that derives these conversion functions automatically. Separation of application data structure types from the structured data interchange language used in serializing them by introducing a common intermediate representation seems like good design to me.
I kind of wish there were an extensible runtime type annotation system in the OCaml standard library. I’m not so full of myself as to suggest that Orsetto’s Cf_type
and related logic should be systematically adopted by the OCaml core, but if something like it were to land in Stdlib
at the direction of somebody with better API design skills than me, I would prefer to adopt it in Orsetto rather than continue to maintain my own crazy alternative.