1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
open Util
module type S = sig
type value
type 'a encoder = 'a -> value
val string : string encoder
val int : int encoder
val float : float encoder
val bool : bool encoder
val null : value
val nullable : 'a encoder -> 'a option encoder
val option : 'a encoder -> 'a option encoder
[@@ocaml.deprecated "Use nullable instead."]
val list : 'a encoder -> 'a list encoder
val array : 'a encoder -> 'a array encoder
val obj : (string * value) list encoder
val obj' : (value * value) list encoder
val value : value encoder
val of_to_string : ('a -> string) -> 'a encoder
val encode_value : 'a encoder -> 'a -> value
val encode_string : 'a encoder -> 'a -> string
end
module type Encodeable = sig
type value
val to_string : value -> string
val of_string : string -> value
val of_int : int -> value
val of_float : float -> value
val of_bool : bool -> value
val null : value
val of_list : value list -> value
val of_key_value_pairs : (value * value) list -> value
end
module Make (E : Encodeable) : S with type value = E.value = struct
type value = E.value
type 'a encoder = 'a -> value
let string x = E.of_string x
let int x = E.of_int x
let float x = E.of_float x
let bool x = E.of_bool x
let null = E.null
let nullable encoder = function None -> E.null | Some x -> encoder x
let option = nullable
let list encoder xs = xs |> My_list.map (fun x -> encoder x) |> E.of_list
let array encoder xs =
xs |> Array.to_list |> My_list.map (fun x -> encoder x) |> E.of_list
let obj' xs = E.of_key_value_pairs xs
let obj xs =
xs |> List.map (fun (k, v) -> (E.of_string k, v)) |> E.of_key_value_pairs
let value x = x
let of_to_string to_string x = string (to_string x)
let encode_value encoder x = encoder x
let encode_string encoder x = encoder x |> E.to_string
end