Source file timedesc_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
open Timedesc
module Time_zone = struct
open Time_zone
let of_json json : t option =
let exception Invalid_data in
try
match json with
| `Assoc l ->
let name =
match List.assoc "name" l with
| `String s -> s
| _ -> raise Invalid_data
in
let table_rows =
match List.assoc "table" l with
| `List l -> l
| _ -> raise Invalid_data
in
table_rows
|> List.map (fun row ->
match row with
| `List [ `String s; `Assoc e ] ->
let start = Int64.of_string s in
let is_dst =
match List.assoc "is_dst" e with
| `Bool b -> b
| _ -> raise Invalid_data
in
let offset =
match List.assoc "offset" e with
| `Int x -> x
| _ -> raise Invalid_data
in
let entry = { is_dst; offset } in
(start, entry)
| _ -> raise Invalid_data)
|> Raw.of_transitions ~name
| _ -> raise Invalid_data
with _ -> None
let of_string s = try of_json @@ Yojson.Basic.from_string s with _ -> None
let to_json (t : t) : Yojson.Basic.t =
`Assoc
[
("name", `String (name t));
( "table",
`List
(Raw.to_transition_seq t
|> Seq.map (fun ((start, _), entry) ->
`List
[
`String (Int64.to_string start);
`Assoc
[
("is_dst", `Bool entry.is_dst);
("offset", `Int entry.offset);
];
])
|> List.of_seq) );
]
end