package ocplib-json-typed

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

Source file json_repr.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
(* Representations of JSON documents *)

(************************************************************************)
(*  ocplib-json-typed                                                   *)
(*                                                                      *)
(*    Copyright 2014 OCamlPro                                           *)
(*                                                                      *)
(*  This file is distributed under the terms of the GNU Lesser General  *)
(*  Public License as published by the Free Software Foundation; either *)
(*  version 2.1 of the License, or (at your option) any later version,  *)
(*  with the OCaml static compilation exception.                        *)
(*                                                                      *)
(*  ocplib-json-typed is distributed in the hope that it will be useful,*)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of      *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *)
(*  GNU General Public License for more details.                        *)
(*                                                                      *)
(************************************************************************)

(*-- types and errors --------------------------------------------------------*)

type document =
  [ `O of (string * value) list
  | `A of value list ]

and value =
  [ `O of (string * value) list
  | `A of value list
  | `Bool of bool
  | `Float of float
  | `String of string
  | `Null ]

type yojson =
  [ `Bool of bool
  | `Assoc of (string * yojson) list
  | `Float of float
  | `Int of int
  | `Intlit of string
  | `List of yojson list
  | `Null
  | `String of string
  | `Tuple of yojson list
  | `Variant of string * yojson option ]

let from_yojson non_basic =
  (* Delete `Variant, `Tuple and `Intlit *)
  let rec to_basic non_basic = match non_basic with
    | `Intlit i -> `String i
    | `Tuple l -> `List (List.map to_basic l)
    | `Variant (label, Some x) -> `List [`String label; to_basic x]
    | `Variant (label, None) -> `String label
    | `Assoc l -> `Assoc (List.map (fun (key, value) -> (key, to_basic value)) l)
    | `List l -> `List (List.map to_basic l)
    | `Int i -> `Int i
    | `Float f -> `Float f
    | `String s -> `String s
    | `Null -> `Null
    | `Bool b -> `Bool b in
  (* Rename `Assoc, `Int and `List *)
  let rec to_value : 'a. _ -> ([> value ] as 'a) = function
    | `List l -> `A (List.map to_value l)
    | `Assoc l -> `O (List.map (fun (key, value) -> (key, to_value value)) l)
    | `Int i -> `Float (float_of_int i)
    | `Float f -> `Float f
    | `Null -> `Null
    | `String s -> `String s
    | `Bool b -> `Bool b in
  to_basic (non_basic :> yojson) |> to_value

let rec to_yojson json =
  let rec aux : 'a. _ -> ([> yojson ] as 'a) = function
    | `A values ->
      `List (List.map aux values)
    | `O values ->
      `Assoc (List.map (fun (k, v) -> (k, aux v)) values)
    | `Float f ->
      let (fract, intr) = modf f in
      let (min_intf, max_intf) = (min_int |> float_of_int,
                                  max_int |> float_of_int) in
      if fract = 0.0 then
        if intr >= min_intf && intr <= max_intf
        then `Int (int_of_float intr)
        else `Intlit (Printf.sprintf "%.0f" intr)
      else `Float f
    | `Bool b -> `Bool b
    | `String s -> `String s
    | `Null -> `Null
  in aux (json :> value)

type path =
  path_item list

and path_item =
  [ `Field of string
  | `Index of int
  | `Star | `Next ]

exception Illegal_pointer_notation of string * int * string
exception Unsupported_path_item of path_item * string
exception Cannot_merge of path

(*-- path operations ---------------------------------------------------------*)

let print_path_as_json_path ?(wildcards = true) ppf = function
  | [] -> Format.fprintf ppf "/"
  | nonempty ->
    let rec print ppf = function
      | [] -> ()
      | `Field n :: rem -> Format.fprintf ppf "/%s%a" n print rem
      | `Index n :: rem -> Format.fprintf ppf "[%d]%a" n print rem
      | `Next :: rem when wildcards -> Format.fprintf ppf "-%a" print rem
      | `Star :: rem when wildcards -> Format.fprintf ppf "*%a" print rem
      | (`Next | `Star) :: _ ->
        raise (Unsupported_path_item (`Star, "JSON path w/o wildcards")) in
    print ppf nonempty

let print_path_as_json_pointer ?(wildcards = true) ppf = function
  | [] -> Format.fprintf ppf "/"
  | nonempty ->
    let rec print ppf = function
      | [] -> ()
      | `Field n :: rem -> Format.fprintf ppf "/%s%a" n print rem
      | `Index n :: rem -> Format.fprintf ppf "/%d%a" n print rem
      | `Next :: rem when wildcards -> Format.fprintf ppf "/-%a" print rem
      | `Next :: _ -> raise (Unsupported_path_item (`Star, "JSON pointer w/o wildcards"))
      | `Star :: _ -> raise (Unsupported_path_item (`Star, "JSON pointer")) in
    print ppf nonempty

let json_pointer_of_path ?wildcards path =
  Format.asprintf "%a" (print_path_as_json_pointer ?wildcards) path

let path_of_json_pointer ?(wildcards = true) str =
  let buf = Buffer.create 100 in
  let len = String.length str in
  let rec slashes acc i =
    if i >= len then List.rev acc
    else if String.get str i = '/' then slashes acc (i + 1)
    else item acc i
  and item acc i =
    if i >= len then List.rev (interp () :: acc)
    else match String.get str i with
      | '/' -> slashes (interp () :: acc) i
      | '~' ->
        if i + 1 >= len then
          raise (Illegal_pointer_notation (str, i, "Unterminated escape sequence")) ;
        begin match String.get str i with
          | '0' -> Buffer.add_char buf '~'
          | '1' -> Buffer.add_char buf '/'
          | _illegal ->
            raise (Illegal_pointer_notation (str, i + 1, "Illegal escape character")) end ;
        item acc (i + 1)
      | unescaped ->
        Buffer.add_char buf unescaped ;
        item acc (i + 1)
  and interp () =
    let field = Buffer.contents buf in
    Buffer.clear buf ;
    if field = "-" then
      if wildcards then
        `Next
      else
        raise (Unsupported_path_item (`Next, "JSON pointer w/o wildcards"))
    else try `Index (int_of_string field) with
      | _ -> `Field field in
  if len = 0 then []
  else if String.get str 0 <> '/' then
    raise (Illegal_pointer_notation (str, 0, "Missing initial slash"))
  else slashes [] 1

(*-- queries -----------------------------------------------------------------*)

let rec query
  : 'a. path -> ([< value ] as 'a) -> value
  = fun path json -> match path, json with
    | [], json ->
      (json :> value)
    | `Field n :: rempath, `O ((n', v) :: rem) ->
      if n = n' then query rempath v else query path (`O rem)
    | `Index i :: rempath, `A cells ->
      let i = if i < 0 then List.length cells - i  else i in
      query rempath (List.nth cells i)
    | `Star :: rempath, `O ((n, v) :: rem) ->
      begin try query rempath v with Not_found -> query path (`O rem) end
    | `Star :: rempath, `A (v :: rem) ->
      begin try query rempath v with Not_found -> query path (`A rem) end
    | _, _ -> raise Not_found

let query_all path json =
  let res = ref [] in
  let rec query
    : 'a. path -> ([< value ] as 'a) -> unit
    = fun path json -> match path, json with
      | [], json ->
        res := (json :> value) :: !res
      | `Field n :: rempath, `O ((n', v) :: rem) ->
        if n = n' then query rempath v else query path (`O rem)
      | `Index i :: rempath, `A cells ->
        let i = if i < 0 then List.length cells - i  else i in
        query rempath (List.nth cells i)
      | `Star :: rempath, `O fields ->
        List.iter (fun (_, v) -> query rempath v) fields
      | `Star :: rempath, `A cells ->
        List.iter (query rempath) cells
      | _, _ -> () in
  query path json ; !res

(*-- updates -----------------------------------------------------------------*)

let sort_fields =
  List.sort (fun (l, _) (r, _) -> compare l r)

let rec canon = function
  | `O l -> `O (List.map (fun (n, o) -> n, canon o) l |> sort_fields)
  | `A l -> `A (List.map canon l)
  | imm -> imm

let equals l r =
  canon l = canon r

let merge l r =
  let rec merge path l r =
    match l, r with
    | `O l, `O r -> `O (merge_fields path [] (sort_fields (l @ r)))
    | `Null, v | v, `Null -> v
    | `A l, `A r -> `A (merge_cells path 0 [] l r)
    | vl, vr when equals vl vr -> vl
    | _ -> raise (Cannot_merge (List.rev path))
  and merge_cells path i acc l r = match l, r with
    | [], rem | rem, [] -> List.rev_append acc rem
    | l :: ls, r :: rs ->
      let item = merge (`Index i :: path) l r in
      merge_cells path (succ i) (item :: acc) ls rs
  and merge_fields path acc = function
    | (lf, lv) :: ((rf, rv) :: rem as rrem) ->
      if lf = rf then
        let item = merge (`Field lf :: path) lv rv in
        merge_fields path ((lf, item) :: acc) rem
      else
        merge_fields path ((lf, lv) :: acc) rrem
    | [ _ ] | [] as last -> last in
  merge [] (l :> value) (r :> value)

let insert ?(merge = merge) path value root =
  let revpath sub =
    let rec loop acc = function
      | l when l == sub -> List.rev acc
      | item :: items -> loop (item :: acc) items
      | [] -> (* absurd *) assert false
    in loop [] path in
  let merge path l r =
    try merge l r with
      Cannot_merge sub -> raise (Cannot_merge (revpath path @ sub)) in
  let value = (value :> value) in
  let root = (root :> value) in
  let rec nulls acc n last =
    if n <= 0 then
      List.rev (last :: acc)
    else
      nulls (`Null :: acc) (pred n) last in
  let rec insert ?root path = match path, root with
    (* create objects *)
    | `Field n :: rempath, None ->
      `O [ (n, insert rempath) ]
    | (`Index 0  | `Star | `Next) :: rempath, None ->
      `A [ insert rempath ]
    | `Index i :: rempath, None ->
      if i < 0 then raise (Cannot_merge (revpath path)) ;
      `A (nulls [] (max 0 (pred i)) (insert rempath))
    | [], None -> value
    (* insert in existing *)
    | [], Some value' ->
      merge path value value'
    | `Field n :: rempath, Some (`O fields) ->
      `O (insert_fields [] n rempath fields)
    | `Index i :: rempath, Some (`A cells) ->
      let i = if i < 0 then List.length cells - i else i in
      if i < 0 then raise (Cannot_merge (revpath path)) ;
      `A (insert_cells [] i rempath cells)
    | `Next :: rempath, Some (`A cells) ->
      `A (List.rev_append (List.rev cells) [ insert rempath ])
    (* multiple insertions *)
    | `Star :: rempath, Some (`A cells) ->
      `A (List.map (fun root -> insert ~root rempath) cells)
    | `Star :: rempath, Some (`O fields) ->
      `O (List.map (fun (n, root) -> (n, insert ~root rempath)) fields)
    | [ `Star ], Some root ->
      merge path value root
    (* FIXME: make explicit unhandled cases *)
    | _, Some _ -> raise (Cannot_merge (revpath path))
  and insert_fields acc n rempath fields = match fields with
    | [] ->
      List.rev ((n, insert rempath) :: acc)
    | (n', root) :: rem when n = n' ->
      List.rev_append ((n, insert ~root rempath) :: acc) rem
    | other :: rem ->
      insert_fields (other :: acc) n rempath rem
  and insert_cells acc n rempath cells =
    match cells, n with
    | [], n ->
      nulls acc n (insert rempath)
    | root :: rem, 0 ->
      List.rev_append ((insert ~root rempath) :: acc) rem
    | other :: rem, n ->
      insert_cells (other :: acc) (n - 1) rempath rem in
  insert ~root path

let replace path value root =
  insert ~merge:(fun value _prev -> value) path value root

let insert path value root =
  insert path value root

let path_operator_name = function
  | `Field _ -> "field access"
  | `Index _ -> "array access"
  | `Star -> "wildcard"
  | `Next -> "array append"

let rec print_error ?print_unknown ppf err = match err with
  | Illegal_pointer_notation (notation, pos, msg) ->
    Format.fprintf ppf
      "@[<v 2>Illegal pointer notation@,At character %d of %S@,%s@]"
      pos notation msg
  | Unsupported_path_item (item, msg) ->
    Format.fprintf ppf
      "Path operator %s unsupported by %s"
      (path_operator_name item) msg
  | Cannot_merge [] ->
    Format.fprintf ppf
      "Unmergeable objects"
  | Cannot_merge path ->
    Format.fprintf ppf
      "Unmergeable objects, incompatibility at %a"
      (print_path_as_json_path ~wildcards:true) path
  | exn ->
    match print_unknown with
    | Some print_unknown -> print_unknown ppf exn
    | None ->
      Format.fprintf ppf "Unhandled error %s" (Printexc.to_string exn)