Reputation: 1812
I am using Core and atdgen in my project.
I have the following record, that I want to serialize in JSON:
type person = {
(* ... *)
birth : Time.t;
(* ... *)
}
Is it possible to tell in my atd file how to serialize Time.t
values into strings (by calling Time.to_string
)?
Upvotes: 1
Views: 246
Reputation: 1812
Although @everiq's answer works, there is a much simpler method for converting JSON strings into custom types. At least for my case.
type time = string wrap <ocaml t="Core.Std.Time.t"
wrap="Core.Std.Time.of_string" unwrap="Core.Std.Time.to_string">
type person = {
(* ... *)
birth : time;
(* ... *)
}
Upvotes: 3
Reputation: 149
You can wrap Time.t in a module like that:
type time <ocaml_json module="TimeWrapper" t="t"> = abstract
and then define your own read/write functions in TimeWrapper:
(* timeWrapper.mli *)
type t
val read_t : Yojson.Safe.lexer_state -> Lexing.lexbuf -> t
val write_t : Bi_outbuf.t -> t -> unit
Upvotes: 0