-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdecode.ml
More file actions
30 lines (27 loc) · 893 Bytes
/
Copy pathdecode.ml
File metadata and controls
30 lines (27 loc) · 893 Bytes
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
(* Decoding a fixed JSON data structure using Melange_json.Of_json *)
let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str =
let json = Melange_json.of_string str in
Melange_json.Of_json.(js_dict decoder json)
|> Js.Dict.map ~f:(fun[@u] v -> f v)
|> Melange_json.To_json.js_dict encoder
|> Melange_json.to_string
let sum = Array.fold_left ( + ) 0
(* prints `{ "foo": 6, "bar": 24 }` *)
let () =
Js.log
@@ mapJsonObjectString sum
Melange_json.Of_json.(array int)
Melange_json.To_json.int
{|
{
"foo": [1, 2, 3],
"bar": [9, 8, 7]
}
|}
(* Error handling *)
let () =
let json = {|{ "y": 42 } |} |> Melange_json.of_string in
match Melange_json.Of_json.(field "x" int json) with
| x -> Js.log x
| exception Melange_json.Of_json_error err ->
Js.log ("Error:" ^ Melange_json.of_json_error_to_string err)