I’m trying to use ppx_deriving_yojson.
The generated helpers functions work well, even in the Toplevel. But several key points are still unclear to me:
(* Example*) type foo = ... val foo_to_yojson : foo -> Yojson.Safe.t = <fun> val foo_of_yojson : Yojson.Safe.t -> foo Ppx_deriving_yojson_runtime.error_or = <fun> val _ : Yojson.Safe.t -> foo Ppx_deriving_yojson_runtime.error_or = <fun>
The program is built with an ad hoc Makefile.
How should I install the package
ppx_deriving_json.runtime? (whether it should be “installed”)
Trying to install it with opam sends me
[ERROR] Package ppx_deriving_yojson has no version runtime.
However, if I specify that
ppx_deriving_json.runtime package in my Makefile, then the program compiles.
But it also compiles if I don’t specify it. So I can just suppose that some error may not be handled at runtime without any alert. And if I forget to specify this package, I will be at risk without any warning.
How can I check that I use correctly this ppx_deriving_yojson package, and especially that it is really used?
To enforce safety, how can I prevent my program from being compiled if the required packages are not present?
BTW, there should be a typo in this section https://github.com/ocaml-ppx/ppx_deriving_yojson#usage
According to the signature,
val foo_of_yojson : Yojson.Safe.t -> foo Ppx_deriving_yojson_runtime.error_or = <fun>
that should somehow be handled as follows:
... >>= fun x_str -> try%lwt some_search_function (* may return Not_found *) >>= fun () -> let x = ( let x_result = foo_of_yojson (Yojson.Safe.from_string x_str) in ( function | Result.Ok y -> y | Result.Error _ -> raise Deriving_Yojson.Failed ) x_result ) in some_function x with | Not_found -> do_something1 | Deriving_Yojson.Failed -> do_something2 | _ -> assert false (* it shows the logic; there may be some typos *)
Deriving_Yojson.Failed is certainly an old ppx_deriving_yojson syntax.
Which syntax should be exactly used? (I could read some >>= and >|= in the source code, but I’m confused and I’m not sure what I should exactly do).
3/ what is exactly this third generated function _ : Yojson.Safe.t -> foo Ppx_deriving_yojson_runtime.error_or ?
(which has the same signature as error_of_yojson)
BTW, it is not documented on https://github.com/ocaml-ppx/ppx_deriving_yojson
4/ How is it possible to get the code of the generated helpers functions, in order to check them?