Marshall -> JSON

I’m trying to rewrite code that uses Marshall (not written by me) to code that uses JSON. Is there a simple / straight forward way to do this ?

Simplest way would probably be to slap @@deriving yojson_conv on the types in your program (using GitHub - janestreet/ppx_yojson_conv: [@@deriving] plugin to generate Yojson conversion functions), and then use the conversion functions to replace the corresponding calls to Marshall functions.

1 Like

I like this idea. One problem: how do I handle situations where some type I depend on is in a library but does not have @@deriving yojson_conv ?

For example, I have some code that depends on types from Merlin-lib , those types in Merlin-lib do not have @@deriving yojson_conv. Is there a way to patch this externally or do I now need to maintain my local fork of Merlin-lib ?

If the type is not abstract, then you can use ppx_import to define conversions for external types (GitHub - ocaml-ppx/ppx_import: Less redundancy in type declarations and signatures).

The readme for ppx_import has a section about doing this: GitHub - ocaml-ppx/ppx_import: Less redundancy in type declarations and signatures

type%import longident = Longident.t [@@deriving show]
let () =
  print_endline (show_longident (Longident.parse "Foo.Bar.baz"))
(* Longident.Ldot (Longident.Ldot (Longident.Lident ("Foo"), "Bar"), "baz") *)

If the type is abstract, then you’re out of luck I think, you’ll just have to maintain your local fork.

1 Like