Source file type_json.ml
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
open Type_core
module Encode = struct
let lexeme e l = ignore (Jsonm.encode e (`Lexeme l))
let unit e () =
lexeme e `Os;
lexeme e `Oe
let base64 e s =
let x = Base64.encode_exn s in
lexeme e `Os;
lexeme e (`Name "base64");
lexeme e (`String x);
lexeme e `Oe
let string e s = if is_valid_utf8 s then lexeme e (`String s) else base64 e s
let bytes e b =
let s = Bytes.unsafe_to_string b in
string e s
let char e c =
let s = String.make 1 c in
string e s
let float e f = lexeme e (`Float f)
let int e i = float e (float_of_int i)
let int32 e i = float e (Int32.to_float i)
let int64 e i = float e (Int64.to_float i)
let bool e = function false -> float e 0. | _ -> float e 1.
let list l e x =
lexeme e `As;
List.iter (l e) x;
lexeme e `Ae
let array l e x =
lexeme e `As;
Array.iter (l e) x;
lexeme e `Ae
let pair a b e (x, y) =
lexeme e `As;
a e x;
b e y;
lexeme e `Ae
let triple a b c e (x, y, z) =
lexeme e `As;
a e x;
b e y;
c e z;
lexeme e `Ae
let boxed_option o e = function
| None -> lexeme e `Null
| Some x ->
lexeme e `Os;
lexeme e (`Name "some");
o e x;
lexeme e `Oe
let rec t : type a. a t -> a encode_json =
fun ty e ->
match ty with
| Self s -> t s.self_fix e
| Custom c -> c.encode_json e
| Map b -> map b e
| Prim t -> prim t e
| List l -> list (t l.v) e
| Array a -> array (t a.v) e
| Tuple t -> tuple t e
| Option x -> boxed_option (t x) e
| Record r -> record r e
| Variant v -> variant v e
| Var v -> raise (Unbound_type_variable v)
and tuple : type a. a tuple -> a encode_json = function
| Pair (x, y) -> pair (t x) (t y)
| Triple (x, y, z) -> triple (t x) (t y) (t z)
and map : type a b. (a, b) map -> b encode_json =
fun { x; g; _ } e u -> t x e (g u)
and prim : type a. a prim -> a encode_json = function
| Unit -> unit
| Bool -> bool
| Char -> char
| Int -> int
| Int32 -> int32
| Int64 -> int64
| Float -> float
| String _ -> string
| Bytes _ -> bytes
and record : type a. a record -> a encode_json =
fun r e x ->
let fields = fields r in
lexeme e `Os;
List.iter
(fun (Field f) ->
match (f.ftype, f.fget x) with
| Option _, None -> ()
| Option o, Some x ->
lexeme e (`Name f.fname);
t o e x
| List _, [] -> ()
| tx, x ->
lexeme e (`Name f.fname);
t tx e x)
fields;
lexeme e `Oe
and variant : type a. a variant -> a encode_json =
fun v e x -> case_v e (v.vget x)
and case_v : type a. a case_v encode_json =
fun e c ->
match c with
| CV0 c -> string e c.cname0
| CV1 (c, v) ->
lexeme e `Os;
lexeme e (`Name c.cname1);
t c.ctype1 e v;
lexeme e `Oe
end
module Decode = struct
let lexeme e =
match Json.decode e with
| `Lexeme e -> Ok e
| `Error e -> Error (`Msg (Fmt.to_to_string Jsonm.pp_error e))
| `End | `Await -> assert false
let ( >>= ) l f = match l with Error _ as e -> e | Ok l -> f l
let ( >|= ) l f = match l with Ok l -> Ok (f l) | Error _ as e -> e
let error e got expected =
let _, (l, c) = Jsonm.decoded_range e.Json.d in
Error
(`Msg
(Fmt.strf
"line %d, character %d:\nFound lexeme %a, but lexeme %s was expected"
l c Jsonm.pp_lexeme got expected))
let expect_lexeme e expected =
lexeme e >>= fun got ->
if expected = got then Ok ()
else error e got (Fmt.to_to_string Jsonm.pp_lexeme expected)
let value e =
let lexemes = ref [] in
let objs = ref 0 in
let arrs = ref 0 in
let rec aux () =
lexeme e >>= fun l ->
lexemes := l :: !lexemes;
let () =
match l with
| `Os -> incr objs
| `As -> incr arrs
| `Oe -> decr objs
| `Ae -> decr arrs
| `Name _ | `Null | `Bool _ | `String _ | `Float _ -> ()
in
if !objs > 0 || !arrs > 0 then aux () else Ok ()
in
aux () >|= fun () -> List.rev !lexemes
let unit e = expect_lexeme e `Os >>= fun () -> expect_lexeme e `Oe
let get_base64_value e =
match lexeme e with
| Ok (`Name "base64") -> (
match lexeme e with
| Ok (`String b) -> (
match expect_lexeme e `Oe with
| Ok () -> Ok (Base64.decode_exn b)
| Error e -> Error e )
| Ok l -> error e l "Bad base64 encoded character"
| Error e -> Error e )
| Ok l -> error e l "Invalid base64 object"
| Error e -> Error e
let string e =
lexeme e >>= function
| `String s -> Ok s
| `Os -> get_base64_value e
| l -> error e l "`String"
let bytes e =
lexeme e >>= function
| `String s -> Ok (Bytes.unsafe_of_string s)
| `Os -> (
match get_base64_value e with
| Ok s -> Ok (Bytes.unsafe_of_string s)
| Error e -> Error e )
| l -> error e l "`String"
let float e =
lexeme e >>= function `Float f -> Ok f | l -> error e l "`Float"
let char e =
lexeme e >>= function
| `String s when String.length s = 1 -> Ok s.[0]
| `Os -> (
match get_base64_value e with Ok s -> Ok s.[0] | Error x -> Error x )
| l -> error e l "`String[0]"
let int32 e = float e >|= Int32.of_float
let int64 e = float e >|= Int64.of_float
let int e = float e >|= int_of_float
let bool e = int e >|= function 0 -> false | _ -> true
let list l e =
expect_lexeme e `As >>= fun () ->
let rec aux acc =
lexeme e >>= function
| `Ae -> Ok (List.rev acc)
| lex ->
Json.rewind e lex;
l e >>= fun v -> aux (v :: acc)
in
aux []
let array l e = list l e >|= Array.of_list
let pair a b e =
expect_lexeme e `As >>= fun () ->
a e >>= fun x ->
b e >>= fun y ->
expect_lexeme e `Ae >|= fun () -> (x, y)
let triple a b c e =
expect_lexeme e `As >>= fun () ->
a e >>= fun x ->
b e >>= fun y ->
c e >>= fun z ->
expect_lexeme e `Ae >|= fun () -> (x, y, z)
let unboxed_option o e = o e >|= fun v -> Some v
let boxed_option o e =
lexeme e >>= function
| `Null -> Ok None
| `Os ->
expect_lexeme e (`Name "some") >>= fun () ->
o e >>= fun v ->
expect_lexeme e `Oe >|= fun () -> Some v
| l -> error e l "`Option-contents"
let rec t : type a. a t -> a decode_json =
fun ty d ->
match ty with
| Self s -> t s.self_fix d
| Custom c -> c.decode_json d
| Map b -> map b d
| Prim t -> prim t d
| List l -> list (t l.v) d
| Array a -> array (t a.v) d
| Tuple t -> tuple t d
| Option x -> boxed_option (t x) d
| Record r -> record r d
| Variant v -> variant v d
| Var v -> raise (Unbound_type_variable v)
and inside_record_t : type a. a t -> a decode_json =
fun ty d -> match ty with Option x -> unboxed_option (t x) d | _ -> t ty d
and tuple : type a. a tuple -> a decode_json = function
| Pair (x, y) -> pair (t x) (t y)
| Triple (x, y, z) -> triple (t x) (t y) (t z)
and map : type a b. (a, b) map -> b decode_json =
fun { x; f; _ } e -> t x e >|= f
and prim : type a. a prim -> a decode_json = function
| Unit -> unit
| Bool -> bool
| Char -> char
| Int -> int
| Int32 -> int32
| Int64 -> int64
| Float -> float
| String _ -> string
| Bytes _ -> bytes
and record : type a. a record -> a decode_json =
fun r e ->
expect_lexeme e `Os >>= fun () ->
let rec soup acc =
lexeme e >>= function
| `Name n -> value e >>= fun s -> soup ((n, s) :: acc)
| `Oe -> Ok acc
| l -> error e l "`Record-contents"
in
soup [] >>= fun soup ->
let rec aux : type a b. (a, b) fields -> b -> (a, [ `Msg of string ]) result
=
fun f c ->
match f with
| F0 -> Ok c
| F1 (h, f) -> (
let v =
try
let s = List.assoc h.fname soup in
let e = Json.decoder_of_lexemes s in
inside_record_t h.ftype e
with Not_found -> (
match h.ftype with
| Option _ -> Ok None
| List _ -> Ok []
| _ ->
Error
(`Msg (Fmt.strf "missing value for %s.%s" r.rname h.fname)) )
in
match v with Ok v -> aux f (c v) | Error _ as e -> e )
in
let (Fields (f, c)) = r.rfields in
aux f c
and variant : type a. a variant -> a decode_json =
fun v e ->
lexeme e >>= function
| `String s -> case0 s v e
| `Os -> case1 v e
| l -> error e l "(`String | `Os)"
and case0 : type a. string -> a variant -> a decode_json =
fun s v _e ->
let rec aux i =
match v.vcases.(i) with
| C0 c when String.compare c.cname0 s = 0 -> Ok c.c0
| _ ->
if i < Array.length v.vcases then aux (i + 1)
else Error (`Msg "variant")
in
aux 0
and case1 : type a. a variant -> a decode_json =
fun v e ->
lexeme e >>= function
| `Name s ->
let rec aux i =
match v.vcases.(i) with
| C1 c when String.compare c.cname1 s = 0 -> t c.ctype1 e >|= c.c1
| _ ->
if i < Array.length v.vcases then aux (i + 1)
else Error (`Msg "variant")
in
aux 0 >>= fun c ->
expect_lexeme e `Oe >|= fun () -> c
| l -> error e l "`Name"
end
let encode = Encode.t
let decode = Decode.t
let decode_jsonm x d = Decode.(t x @@ { Json.d; lexemes = [] })
let pp ?minify t ppf x =
let buf = Buffer.create 42 in
let e = Jsonm.encoder ?minify (`Buffer buf) in
encode t e x;
ignore (Jsonm.encode e `End);
Fmt.string ppf (Buffer.contents buf)
let to_string ?minify t x = Fmt.to_to_string (pp ?minify t) x
let of_string x s = Decode.(t x @@ Json.decoder (`String s))
let decode_lexemes x ls = Decode.(t x @@ Json.decoder_of_lexemes ls)