I haven’t used it myself, but ppx_import claims to be able to do something like this in its README. ppx_importdoes apparently require a (slower?) staged preprocessing setup in dune though.
EDIT: I guess this works on single types rather than modules though.
Since Bignum.t is an abstract type, you’ll need to define of_yojson and to_yojson functions yourself:
type bignum = Bignum.t
let bignum_to_yojson n = `String (Bignum.to_string_accurate n)
let bignum_of_yojson = function
| `String n -> (
try Ok (Bignum.of_string n)
with e ->
Error (Format.sprintf "Invalid Bignum: %s" (Printexc.to_string e)) )
| json ->
Error (Format.sprintf "Invalid Bignum: %s" (Yojson.Safe.to_string json))
type t = {value: bignum} [@@deriving yojson]
When you’re doing this for a non-abstract type, you can use ppx_import or simply duplicate the type:
module Person = struct
type t =
{ first_name: string
; last_name: string }
end
module Company = struct
type person = Person.t =
{ first_name: string
; last_name: string }
[@@deriving yojson]
type t =
{ employees: person list
; address: string }
[@@deriving yojson]
end