module Deriving_Json:sig..end
type 'a t
'a.val to_string : 'a t -> 'a -> stringto_string Json.t<ty> v marshal the v of type ty to a JSON string.val from_string : 'a t -> string -> 'afrom_string Json.t<ty> s safely unmarshal the JSON s into an
OCaml value of type ty. Throws Failure if the received value
isn't the javascript representation of a value of type ty.module type Json =sig..end
val convert : 'a t -> ('a -> 'b) -> ('b -> 'a) -> 'b tconvert (t : 'a t) (from_ : 'a -> 'b) (to_ : 'b -> 'a)
generate a JSON parser/printer for value of type 'b using the parser/printer of type 'a
provided by tmodule type Json_converter =sig..end
module Convert(J:Json_converter):Jsonwith type a = J.b
(* My map module *)
module StringMap = Map.Make(String)
(* Use deriving_json syntax to generate the JSON class for the array of tuple *)
type 'a t = (string * 'a) array deriving (Json)
(* generate the JSON class for StringMap *)
module Json_string_map_t(A : Deriving_Json.Json) : Deriving_Json.Json with type a = A.a StringMap.t = struct
module S = Json_t(A)
include Deriving_Json.Convert(struct
type a = A.a t
type b = A.a StringMap.t
let t = S.t
let to_ : b -> A.a t = fun a -> Array.of_list (StringMap.bindings a)
let from_ : A.a t -> b = fun l ->
Array.fold_left
(fun map (x,v) -> StringMap.add x v map)
StringMap.empty
l
end)
end
You can then ask the syntax extension to use the JSON class Json_string_map_t for StringMap.t
by registering an alias
Pa_deriving_Json.register_predefs ["StringMap";"t"] ["string_map_t"](* My map module *)
module StringMap = Map.Make(String)
(* Use deriving_json syntax to generate the JSON class for the array of tuple *)
type 'a t = (string * 'a) array deriving (Json)
(* generate the JSON class for StringMap *)
module Json_string_map_t(A : Deriving_Json.Json) : Deriving_Json.Json with type a = A.a StringMap.t = struct
module S = Json_t(A)
include Deriving_Json.Convert(struct
type a = A.a t
type b = A.a StringMap.t
let t = S.t
let to_ : b -> A.a t = fun a -> Array.of_list (StringMap.bindings a)
let from_ : A.a t -> b = fun l ->
Array.fold_left
(fun map (x,v) -> StringMap.add x v map)
StringMap.empty
l
end)
end
You can then ask the syntax extension to use the JSON class Json_string_map_t for StringMap.t
by registering an alias
Pa_deriving_Json.register_predefs ["StringMap";"t"] ["string_map_t"]module type Json_min =sig..end
module type Json_min' =sig..end
module type Json_min'' =sig..end
module Defaults(J:Json_min):Jsonwith type a = J.a
module Defaults'(J:Json_min'):Jsonwith type a = J.a
module Defaults''(J:Json_min''):Jsonwith type a = J.a
module Json_char:Jsonwith type a = char
module Json_bool:Jsonwith type a = bool
module Json_unit:Jsonwith type a = unit
module Json_int:Jsonwith type a = int
module Json_int32:Jsonwith type a = int32
module Json_int64:Jsonwith type a = int64
module Json_nativeint:Jsonwith type a = nativeint
module Json_float:Jsonwith type a = float
module Json_string:Jsonwith type a = string
module Json_list(A:Json):Jsonwith type a = A.a list
module Json_ref(A:Json):Jsonwith type a = A.a ref
module Json_option(A:Json):Jsonwith type a = A.a option
module Json_array(A:Json):Jsonwith type a = A.a array