-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtree.ml
More file actions
62 lines (54 loc) · 1.28 KB
/
Copy pathtree.ml
File metadata and controls
62 lines (54 loc) · 1.28 KB
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
(* Decode a JSON tree structure *)
type 'a tree = Node of 'a * 'a tree list | Leaf of 'a
module Decode = struct
open Jsonkit.Of_json
let rec tree decoder json =
let node_type = field "type" string json in
match node_type with
| "node" -> node decoder json
| "leaf" -> leaf decoder json
| _ -> failwith "unknown node type"
and node decoder json =
Node
( json |> field "value" decoder,
json
|> field "children" (array (tree decoder) |> map Array.to_list) )
and leaf decoder json = Leaf (json |> field "value" decoder)
end
let rec indent = function
| n when n <= 0 -> ()
| n ->
print_string " ";
indent (n - 1)
let print =
let rec aux level = function
| Node (value, children) ->
indent level;
Js.log value;
children |> List.iter (fun child -> aux (level + 1) child)
| Leaf value ->
indent level;
Js.log value
in
aux 0
let json =
{| {
"type": "node",
"value": 9,
"children": [{
"type": "node",
"value": 5,
"children": [{
"type": "leaf",
"value": 3
}, {
"type": "leaf",
"value": 2
}]
}, {
"type": "leaf",
"value": 4
}]
} |}
let myTree =
json |> Jsonkit.of_string |> Decode.tree Jsonkit.Of_json.int |> print