-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJson_decode_extended.ml
79 lines (63 loc) · 1.73 KB
/
Json_decode_extended.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
include Json.Decode
external _stringify : Js.Json.t -> string = "JSON.stringify" [@@bs.val]
let succeed any json = any
let index i decode json =
if Js.Array.isArray json then begin
let source = (Obj.magic (json : Js.Json.t) : Js.Json.t array) in
decode (Array.unsafe_get source i)
end
else
raise @@ DecodeError ("Expected array, got " ^ _stringify json)
let map2 f d1 d2 json =
f (d1 json) (d2 json)
let map3 f d1 d2 d3 json =
f (d1 json) (d2 json) (d3 json)
let map4 f d1 d2 d3 d4 json =
f (d1 json) (d2 json) (d3 json) (d4 json)
let variant0 constructor = succeed constructor
let variant1 constructor d1 =
map constructor
(index 1 d1)
let variant2 constructor d1 d2 =
map2 constructor
(index 1 d1)
(index 2 d2)
let variant3 constructor d1 d2 d3 =
map3 constructor
(index 1 d1)
(index 2 d2)
(index 3 d3)
let variant4 constructor d1 d2 d3 d4 =
map4 constructor
(index 1 d1)
(index 2 d2)
(index 3 d3)
(index 4 d4)
let variants decoders =
let constructors =
decoders
|. Belt.List.map (fun (a, _) -> a)
|> String.concat ", "
in
index 0 string
|> andThen
(fun str_constructor ->
(match Belt.List.getAssoc decoders str_constructor (=) with
| Some decode ->
decode
| None ->
raise @@ DecodeError ("Got " ^ (str_constructor) ^ ", expected one of " ^ constructors)))
let orNull decoder default json =
if (Obj.magic json : 'a Js.null) == Js.null then
default
else
decoder json
let dict decoder json =
dict decoder json
|> Js.Dict.entries
|> Belt.Map.String.fromArray
let decodeString decoder str =
try
Belt.Result.Ok (decoder (Json.parseOrRaise str))
with e ->
Belt.Result.Error (Printexc.to_string e)