package data-encoding

  1. Overview
  2. Docs

Source file 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

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

type schema = Json_schema.schema

type pair_builder = {
  build :
    'a 'b.
    Encoding.Kind.t -> 'a Encoding.t -> 'b Encoding.t -> ('a * 'b) Encoding.t;
}

exception Parse_error of string

let wrap_error f str =
  try f str with
  | (Out_of_memory | Stack_overflow) as e -> raise e
  | exn -> raise (Json_encoding.Cannot_destruct ([], exn))

let int64_encoding =
  let open Json_encoding in
  def
    "int64"
    ~title:"64 bit integers"
    ~description:"Decimal representation of 64 bit integers"
  @@ conv Int64.to_string (wrap_error Int64.of_string) string

let n_encoding =
  let open Json_encoding in
  def
    "positive_bignum"
    ~title:"Positive big number"
    ~description:"Decimal representation of a positive big number"
  @@ conv
       (fun z ->
         if Z.sign z < 0 then invalid_arg "negative natural" ;
         Z.to_string z)
       (fun s ->
         let n = Z.of_string s in
         if Z.sign n < 0 then
           raise
             (Json_encoding.Cannot_destruct ([], Failure "negative natural")) ;
         n)
       string

let z_encoding =
  let open Json_encoding in
  def
    "bignum"
    ~title:"Big number"
    ~description:"Decimal representation of a big number"
  @@ conv Z.to_string Z.of_string string

let jsont_as_hex to_hex of_hex =
  let open Json_encoding in
  let schema =
    let open Json_schema in
    create
      {
        title = None;
        description = None;
        default = None;
        enum = None;
        kind =
          String
            {
              str_format = None;
              (* hex encoding uses two hex-digits for each byte, leading to the
                 following regexp *)
              pattern = Some "^([a-zA-Z0-9][a-zA-Z0-9])*$";
              min_length = 0;
              max_length = None;
            };
        format = None;
        id = None;
      }
  in
  conv
    ~schema
    (fun h ->
      let (`Hex s) = to_hex h in
      s)
    (fun h -> wrap_error of_hex (`Hex h))
    string

let bytes_as_hex_jsont = jsont_as_hex Hex.of_bytes Hex.to_bytes

let string_as_hex_jsont = jsont_as_hex Hex.of_string Hex.to_string

let check_utf8 s =
  Uutf.String.fold_utf_8
    (fun valid _pos -> function `Uchar _ -> valid | `Malformed _ -> false)
    true
    s

let raw_string_encoding =
  let open Json_encoding in
  let utf8_case =
    case string (fun s -> if check_utf8 s then Some s else None) (fun s -> s)
  in
  let obj_case =
    case
      (obj1
         (req
            "invalid_utf8_string"
            (array (ranged_int ~minimum:0 ~maximum:255 "byte"))))
      (fun s -> Some (Array.init (String.length s) (fun i -> Char.code s.[i])))
      (fun a -> String.init (Array.length a) (fun i -> Char.chr a.(i)))
  in
  def
    "unistring"
    ~title:"Universal string representation"
    ~description:
      "Either a plain UTF8 string, or a sequence of bytes for strings that \
       contain invalid byte sequences."
    (union [utf8_case; obj_case])

let raw_bytes_encoding =
  Json_encoding.conv Bytes.to_string Bytes.of_string raw_string_encoding

let rec lift_union : type a. a Encoding.t -> a Encoding.t =
 fun e ->
  let open Encoding in
  match e.encoding with
  | Conv {proj; inj; encoding = e; schema} -> (
      match lift_union e with
      | {encoding = Union {kind; tag_size; tagged_cases; cases; match_case}; _}
        ->
          let match_case x = match_case (proj x) in
          let lift
              (Case
                {title; description; encoding; proj = proj'; inj = inj'; tag}) =
            Case
              {
                encoding;
                title;
                description;
                proj = (fun x -> proj' (proj x));
                inj = (fun x -> inj (inj' x));
                tag;
              }
          in
          make
          @@ Union
               {
                 kind;
                 tag_size;
                 tagged_cases = Array.map lift tagged_cases;
                 match_case;
                 cases = List.map lift cases;
               }
      | e -> make @@ Conv {proj; inj; encoding = e; schema})
  | Objs {kind; left; right} ->
      lift_union_in_pair
        {build = (fun kind left right -> make @@ Objs {kind; left; right})}
        kind
        left
        right
  | Tups {kind; left; right} ->
      lift_union_in_pair
        {build = (fun kind left right -> make @@ Tups {kind; left; right})}
        kind
        left
        right
  | Null | Empty | Ignore | Constant _ | Bool | Int8 | Uint8 | Int16 _
  | Uint16 _ | Int31 _ | Int32 _ | Int64 _ | N | Z | RangedInt _ | RangedFloat _
  | Float | Bytes _ | String _ | Bigstring _
  | Padded (_, _)
  | String_enum (_, _)
  | Array _ | List _ | Obj _ | Tup _ | Union _ | Mu _ | Describe _ | Splitted _
  | Dynamic_size _ | Check_size _ | Delayed _ ->
      e

and lift_union_in_pair :
    type a b.
    pair_builder ->
    Encoding.Kind.t ->
    a Encoding.t ->
    b Encoding.t ->
    (a * b) Encoding.t =
 fun b p e1 e2 ->
  let open Encoding in
  match (lift_union e1, lift_union e2) with
  | e1, {encoding = Union {tag_size; match_case; cases; tagged_cases; _}; _} ->
      let match_case (x, y) =
        match match_case y with
        | Matched (tag, e2, v) ->
            Matched (tag, lift_union_in_pair b p e1 e2, (x, v))
      in
      let lift (Case {title; description; encoding = e2; proj; inj; tag}) =
        Case
          {
            encoding = lift_union_in_pair b p e1 e2;
            title;
            description;
            proj =
              (fun (x, y) ->
                match proj y with None -> None | Some y -> Some (x, y));
            inj = (fun (x, y) -> (x, inj y));
            tag;
          }
      in
      make
      @@ Union
           {
             kind = `Dynamic (* ignored *);
             tag_size;
             tagged_cases = Array.map lift tagged_cases;
             match_case;
             cases = List.map lift cases;
           }
  | {encoding = Union {tag_size; tagged_cases; match_case; cases; _}; _}, e2 ->
      let match_case (x, y) =
        match match_case x with
        | Matched (tag, e1, v) ->
            Matched (tag, lift_union_in_pair b p e1 e2, (v, y))
      in
      let lift (Case {title; description; encoding = e1; proj; inj; tag}) =
        Case
          {
            encoding = lift_union_in_pair b p e1 e2;
            title;
            description;
            proj =
              (fun (x, y) ->
                match proj x with None -> None | Some x -> Some (x, y));
            inj = (fun (x, y) -> (inj x, y));
            tag;
          }
      in
      make
      @@ Union
           {
             kind = `Dynamic (* ignored *);
             tag_size;
             tagged_cases = Array.map lift tagged_cases;
             match_case;
             cases = List.map lift cases;
           }
  | e1, e2 -> b.build p e1 e2

let rec json : type a. a Encoding.desc -> a Json_encoding.encoding =
  let open Encoding in
  let open Json_encoding in
  function
  | Null -> null
  | Empty -> empty
  | Constant s -> constant s
  | Ignore -> unit
  | Int8 -> ranged_int ~minimum:~-(1 lsl 7) ~maximum:((1 lsl 7) - 1) "int8"
  | Uint8 -> ranged_int ~minimum:0 ~maximum:((1 lsl 8) - 1) "uint8"
  | Int16 _ ->
      ranged_int ~minimum:~-(1 lsl 15) ~maximum:((1 lsl 15) - 1) "int16"
  | Uint16 _ -> ranged_int ~minimum:0 ~maximum:((1 lsl 16) - 1) "uint16"
  | RangedInt {minimum; endianness = _; maximum} ->
      ranged_int ~minimum ~maximum "rangedInt"
  | Int31 _ -> int
  | Int32 _ -> int32
  | Int64 _ -> int64_encoding
  | N -> n_encoding
  | Z -> z_encoding
  | Bool -> bool
  | Float -> float
  | RangedFloat {minimum; maximum} ->
      ranged_float ~minimum ~maximum "rangedFloat"
  | String (kind, json_repr) -> (
      let f =
        match json_repr with
        | Plain -> raw_string_encoding
        | Hex -> string_as_hex_jsont
      in
      match kind with
      | `Fixed expected ->
          let check s =
            let found = String.length s in
            if found <> expected then
              raise
                (Cannot_destruct
                   ( [],
                     Unexpected
                       ( Format.asprintf "string (len %d)" found,
                         Format.asprintf "string (len %d)" expected ) )) ;
            s
          in

          conv check check f
      | _ -> f)
  | Bigstring (kind, json_repr) -> (
      let f =
        match json_repr with
        | Plain ->
            conv
              Bigstringaf.to_string
              (fun s -> Bigstringaf.of_string ~off:0 ~len:(String.length s) s)
              raw_string_encoding
        | Hex ->
            conv
              Bigstringaf.to_string
              (fun s -> Bigstringaf.of_string ~off:0 ~len:(String.length s) s)
              string_as_hex_jsont
      in
      match kind with
      | `Fixed expected ->
          let check s =
            let found = Bigstringaf.length s in
            if found <> expected then
              raise
                (Cannot_destruct
                   ( [],
                     Unexpected
                       ( Format.asprintf "string (len %d)" found,
                         Format.asprintf "string (len %d)" expected ) )) ;
            s
          in

          conv check check f
      | _ -> f)
  | Padded (e, _) -> get_json e
  | Bytes (kind, json_repr) -> (
      let f =
        match json_repr with
        | Hex -> bytes_as_hex_jsont
        | Plain -> raw_bytes_encoding
      in
      match kind with
      | `Fixed expected ->
          let check s =
            let found = Bytes.length s in
            if found <> expected then
              raise
                (Cannot_destruct
                   ( [],
                     Unexpected
                       ( Format.asprintf "string (len %d)" found,
                         Format.asprintf "string (len %d)" expected ) )) ;
            s
          in
          conv check check f
      | _ -> f)
  | String_enum (tbl, _) ->
      string_enum (Hashtbl.fold (fun a (str, _) acc -> (str, a) :: acc) tbl [])
  | Array {elts = e; length_encoding = _; length_limit = _} ->
      array (get_json e) (* FIXME TODO enforce length_limit *)
  | List {elts = e; length_encoding = _; length_limit = _} ->
      list (get_json e) (* FIXME TODO enforce length_limit *)
  | Obj f -> obj1 (field_json f)
  | Objs {left; right; _} -> merge_objs (get_json left) (get_json right)
  | Tup e -> tup1 (get_json e)
  | Tups {left; right; _} -> merge_tups (get_json left) (get_json right)
  | Conv {proj; inj; encoding = e; schema} -> conv ?schema proj inj (get_json e)
  | Describe {id; title; description; encoding = e} ->
      def id ?title ?description (get_json e)
  | Mu {name; fix; _} as ty ->
      mu name (fun json_encoding -> get_json @@ fix (make ~json_encoding ty))
  | Union {cases; _} -> union (List.map case_json cases)
  | Splitted {json_encoding; _} -> json_encoding
  | Dynamic_size {encoding = e; _} -> get_json e
  | Check_size {encoding; _} -> get_json encoding
  | Delayed f -> get_json (f ())

and field_json : type a. a Encoding.field -> a Json_encoding.field =
  let open Json_encoding in
  function
  | Encoding.Req {name; encoding = e; title; description} ->
      req ?title ?description name (get_json e)
  | Encoding.Opt {name; encoding = e; title; description; kind = _} ->
      opt ?title ?description name (get_json e)
  | Encoding.Dft {name; encoding = e; default = d; title; description} ->
      dft ?title ?description name (get_json e) d

and case_json : type a. a Encoding.case -> a Json_encoding.case =
  let open Json_encoding in
  function
  | Encoding.Case {encoding = e; proj; inj; tag = _; title; description} ->
      case ~title ?description (get_json e) proj inj

and get_json : type a. a Encoding.t -> a Json_encoding.encoding =
 fun e ->
  match e.json_encoding with
  | None ->
      let json_encoding = json (lift_union e).encoding in
      e.json_encoding <- Some json_encoding ;
      json_encoding
  | Some json_encoding -> json_encoding

let convert = get_json

type path = path_item list

and path_item =
  [ `Field of string  (** A field in an object. *)
  | `Index of int  (** An index in an array. *)
  | `Star  (** Any / every field or index. *)
  | `Next  (** The next element after an array. *) ]

include Json_encoding

let construct ?include_default_fields e v =
  construct ?include_default_fields (get_json e) v

type jsonm_lexeme =
  [ `Null
  | `Bool of bool
  | `String of string
  | `Float of float
  | `Name of string
  | `As
  | `Ae
  | `Os
  | `Oe ]

let construct_seq e v = construct_seq (get_json e) v

let destruct ?(bson_relaxation = false) e v =
  destruct ~bson_relaxation (get_json e) v

let schema ?definitions_path e = schema ?definitions_path (get_json e)

let cannot_destruct fmt =
  Format.kasprintf (fun msg -> raise (Cannot_destruct ([], Failure msg))) fmt

type t = json

let to_string ?(newline = false) ?minify j =
  Format.asprintf
    "%a%s"
    Json_repr.(pp ?compact:minify (module Ezjsonm))
    j
    (if newline then "\n" else "")

let pp = Json_repr.(pp (module Ezjsonm))

let from_string s =
  match Ezjsonm.from_string ("[" ^ s ^ "]") with
  | exception Ezjsonm.Parse_error (_, msg) -> Error msg
  | `A [json] -> Ok json
  | `A ([] | _ :: _ :: _) | `O _ -> Error "Malformed value"

let encoding =
  let binary : Json_repr.ezjsonm Encoding.t =
    Encoding.conv
      (fun json ->
        Json_repr.convert
          (module Json_repr.Ezjsonm)
          (module Json_repr_bson.Repr)
          json
        |> Json_repr_bson.bson_to_bytes |> Bytes.to_string)
      (fun s ->
        try
          Bytes.of_string s
          |> Json_repr_bson.bytes_to_bson ~copy:false
          |> Json_repr.convert
               (module Json_repr_bson.Repr)
               (module Json_repr.Ezjsonm)
        with Json_repr_bson.Bson_decoding_error (msg, _, _) ->
          raise (Parse_error msg))
      Encoding.string
  in
  let json = Json_encoding.any_ezjson_value in
  Encoding.raw_splitted ~binary ~json

let schema_encoding =
  Encoding.conv Json_schema.to_json Json_schema.of_json encoding

let bytes : Encoding.string_json_repr -> bytes Json_encoding.encoding = function
  | Encoding.Hex -> bytes_as_hex_jsont
  | Plain -> raw_bytes_encoding

let string = function
  | Encoding.Hex -> string_as_hex_jsont
  | Plain -> raw_string_encoding
OCaml

Innovation. Community. Security.