-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathencode.ml
More file actions
52 lines (44 loc) · 1.2 KB
/
Copy pathencode.ml
File metadata and controls
52 lines (44 loc) · 1.2 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
(* Encoding a JSON data structure using Jsonkit.Encode *)
(* prints ["foo", "bar"] *)
let _ =
[| "foo"; "bar" |]
|> Jsonkit.To_json.string_array
|> Jsonkit.to_string
|> Js.log
(* prints ["foo", "bar"] *)
let _ =
[| "foo"; "bar" |]
|> Js.Array.map ~f:Jsonkit.To_json.string
|> Jsonkit.To_json.json_array
|> Jsonkit.to_string
|> Js.log
(* prints { x: 42, foo: 'bar' } *)
let _ =
Jsonkit.To_json.(
json_dict (Js.Dict.fromList [ "x", int 42; "foo", string "bar" ]))
|> Js.log
(* Advanced example: encode a record *)
type line = { start : point; end_ : point; thickness : int option }
and point = { x : float; y : float }
module Encode = struct
let point r =
let open! Jsonkit.To_json in
json_dict (Js.Dict.fromList [ "x", float r.x; "y", float r.y ])
let line r =
Jsonkit.To_json.(
json_dict
(Js.Dict.fromList
[
"start", point r.start;
"end", point r.end_;
( "thickness",
match r.thickness with Some x -> int x | None -> unit () );
]))
end
let data =
{
start = { x = 1.1; y = -0.4 };
end_ = { x = 5.3; y = 3.8 };
thickness = Some 2;
}
let _ = data |> Encode.line |> Js.log