package atdgen-codec-runtime

  1. Overview
  2. Docs

Source file decode.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
type 'a t = Json.t -> 'a

let make f = f

exception DecoderError

let decode f json = f json

let unit = function
  | `Null -> ()
  | _ -> raise DecoderError

let bool = function
  | `Bool b -> b
  | _ -> raise DecoderError

let int = function
  | `Int i -> i
  | _ -> raise DecoderError

let float = function
  | `Float f -> f
  | _ -> raise DecoderError

let char = function
  | `String s when String.length s = 1 -> s.[0]
  | _ -> raise DecoderError

let string = function
  | `String s -> s
  | _ -> raise DecoderError

let list f = function
  | `List l -> List.map f l
  | _ -> raise DecoderError

let array f = function
  | `List l -> Array.map f (Array.of_list l)
  | _ -> raise DecoderError

let obj_list f = function
  | `Assoc l -> List.map (fun (k, v) -> k, f v) l
  | _ -> raise DecoderError

let obj_array f = function
  | `Assoc l -> Array.map (fun (k, v) -> k, f v) (Array.of_list l)
  | _ -> raise DecoderError

let optional f j =
  match f j with
  | exception DecoderError -> None
  | v -> Some v

let map f c j = f (c j)

let field s f = function
  | `Assoc v -> f (List.assoc s v)
  | _ -> raise DecoderError

let fieldOptional s f = function
  | `Assoc v ->
      begin match List.assoc s v with
        | exception Not_found -> None
        | v -> Some (f v)
      end
  | _ -> raise DecoderError

let fieldDefault s default f =
  fieldOptional s f
  |> map (function
    | None -> default
    | Some s -> s)

let tuple1 a = function
  | `Tuple [w]
  | `List [w] -> a w
  | _ -> raise DecoderError

let tuple2 a b = function
  | `Tuple [w ; x]
  | `List [w ; x] -> (a w, b x)
  | _ -> raise DecoderError

let tuple3 a b c = function
  | `Tuple [w; x; y]
  | `List [w; x; y] -> (a w, b x, c y)
  | _ -> raise DecoderError

let tuple4 a b c d = function
  | `Tuple [w; x; y; z]
  | `List [w; x; y; z] -> (a w, b x, c y, d z)
  | _ -> raise DecoderError

let enum l = function
  | `Variant (s, None)
  | `String s ->
      begin match List.assoc s l with
        | exception Not_found -> raise DecoderError
        | `Single a -> a
        | `Decode _ -> raise DecoderError
      end
  | `Variant (s, Some args)
  | `List [`String s; args] ->
      begin match List.assoc s l with
        | exception Not_found -> raise DecoderError
        | `Single _ -> raise DecoderError
        | `Decode d -> decode d args
      end
  | _ -> raise DecoderError

let option_as_constr f =
  enum
    [ "None", `Single None
    ; "Some", `Decode (map (fun x -> Some x) f)
    ]

let nullable f = function
  | `Null -> None
  | x -> Some (f x)

let adapter (normalize: Json.t -> Json.t) (reader: 'a t) json =
  reader (normalize json)
OCaml

Innovation. Community. Security.