package json_decoder

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file json_decoder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
module Dict = Map.Make(String)

type 'a dict = 'a Dict.t

let always x _ = x

module Results = struct
  let map f = function
    | Result.Ok v -> Result.Ok (f v)
    | Result.Error _ as err -> err

  let bind f = function
    | Result.Ok v -> f v
    | Result.Error _ as err -> err

  let is_ok = function
    | Result.Ok _ -> true
    | Result.Error _ -> false

  let (>>=) t f = bind f t

  let (>>|) t f = map f t

  let (<*>) f t = f >>= fun f -> t >>| f

  let (<$>) f t = t >>| f
end

type value = Yojson.Basic.json

type 'a t =
  | Decoder of ( value -> ('a, string) Result.result )

let describe_value = function
  | `String _ -> "string"
  | `Float _ -> "float"
  | `Int _ -> "int"
  | `Bool _ -> "bool"
  | `Null -> "null"
  | `Assoc _ -> "object"
  | `List _ -> "list"

let value_error s value =
  describe_value value
  |> Printf.sprintf s
  |> ( fun s -> Result.Error s )

let decode ( Decoder f ) value = f value

let string =
  Decoder begin function
    | `String s -> Result.Ok s
    | otherwise -> value_error "%s is not a string" otherwise
  end

let float =
  Decoder begin function
    | `Float f -> Result.Ok f
    | otherwise -> value_error "%s is not a float" otherwise
  end

let int =
  Decoder begin function
    | `Int i -> Result.Ok i
    | otherwise -> value_error "%s is not a int" otherwise
  end

let bool =
  Decoder begin function
    | `Bool b -> Result.Ok b
    | otherwise -> value_error "%s is not a bool" otherwise
  end

let null a =
  Decoder begin function
    | `Null -> Result.Ok a
    | otherwise -> value_error "%s is not null" otherwise
  end

let map f decoder =
  let open Results in
  Decoder begin fun value ->
    decode decoder value >>| f
  end

let list decoder =
  Decoder begin function
    | `List values ->
      let open Results in
      let push x xs = x :: xs in

      List.fold_right begin fun value acc ->
        push <$> decode decoder value <*> acc
      end
        values
        ( Result.Ok [] )
    | otherwise -> value_error "%s is not a list" otherwise
  end

let dict decoder =
  Decoder begin function
    | `Assoc values ->
      let open Results in
      let add key value dct = Dict.add key value dct in

      List.fold_left begin fun dct (key, value) ->
        add key <$> decode decoder value <*> dct
      end
        ( Result.Ok Dict.empty )
        values
    | otherwise -> value_error "%s is not a dict" otherwise
  end

let pairs decoder =
  Decoder begin function
    | `Assoc values ->
      let open Results in
      let push key value sofar = (key, value) :: sofar in

      List.fold_right begin fun (key, value) acc ->
        push key <$> decode decoder value <*> acc
      end
        values
        ( Result.Ok [] )
    | otherwise -> value_error "%s is not a dict" otherwise
  end

let array decoder =
  map Array.of_list ( list decoder )

let field key decoder =
  Decoder begin function
    | `Assoc a ->
      let value = try
          decode decoder @@ List.assoc key a
        with
          Not_found ->
          let keys =
            a
            |> List.map fst
            |> String.concat ", "
          in
          Result.Error ( Printf.sprintf "key %s does not exist in object %s " key keys)
      in
      value
    | otherwise -> value_error "%s is not an object" otherwise
  end

let (@=) = field

let at keys decoder =
  List.fold_right field keys decoder

let index i decoder =
  Decoder begin function
    | `List values ->
      let value = try
          decode decoder @@ List.nth values i
        with Failure _ ->
          Result.Error "index out of bounds"
      in
      value
    | otherwise -> value_error "%s is not a list" otherwise
  end


let one_of decoders =
  Decoder begin fun value ->
    let values =
      List.map ( fun decoder -> decode decoder value ) decoders in
    try
      List.find Results.is_ok values
    with
      Not_found -> Result.Error "no suitable decoder chosen"
  end

let succeed a = Decoder ( always @@ Result.Ok a )

let fail s = Decoder ( always @@ Result.Error s )

let option decoder =
  Decoder begin fun value ->
    let opt =
      match decode decoder value with
      | Result.Ok c -> Some c
      | Result.Error _ -> None
    in
    Result.Ok opt
  end

let value =
  Decoder begin fun value ->
    Result.Ok value
  end

let and_then fn decoder =
  Decoder begin fun value ->
    let ( Decoder callback ) =
      match decode decoder value with
      | Result.Ok c -> fn c
      | Result.Error e -> fail e
    in
    callback value
  end

let (>>=) decoder fn = and_then fn decoder

let (>>|) t f = map f t

let (<*>) f t = f >>= fun f -> t >>| f

let mapN f = succeed f

let (||>) f t = f <*> t

let apply f t = f <*> t

let value_of_string s = Yojson.Basic.from_string s

let value_to_string v = Yojson.Basic.to_string v

let value_to_yojson v = v

let value_of_yojson v = v

let decode_string t s =
  decode t @@ value_of_string s
OCaml

Innovation. Community. Security.