Declarative JSON data manipulation for OCaml
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

json: rename internal Json_base module to Core

The package had a private [Json_base] module holding shared utilities
(Fmt, Sort, Type, Rarray, etc.). Rename to the conventional [Core]
to match the rest of the codebase and drop the redundant package
prefix in the module name.

- Move json_base.ml/.mli to core.ml/.mli (content unchanged).
- Update json.ml to reference Core.* instead of Json_base.*.
- Drop the old (private_modules json_base) clause from lib/dune;
Core remains a public module of the json library.

Also picks up dune-fmt formatting tweaks in json_bytesrw.ml and
fuzz_skip.ml that were pending in the working tree.

+219 -223
+2 -2
fuzz/fuzz_skip.ml
··· 23 23 [ 24 24 ( "differential", 25 25 [ 26 - Crowbar.test_case "json accepts => ignore accepts" 27 - [ Crowbar.bytes ] check_implication; 26 + Crowbar.test_case "json accepts => ignore accepts" [ Crowbar.bytes ] 27 + check_implication; 28 28 ] ); 29 29 ]
+3 -7
lib/bytesrw/json_bytesrw.ml
··· 58 58 (* A simple growable byte buffer used for token and whitespace 59 59 accumulation. Raw [Bytes.t] access lets us compare buffer content 60 60 against candidate keys without allocating an intermediate string. *) 61 - type tokbuf = { 62 - mutable bytes : Stdlib.Bytes.t; 63 - mutable len : int; 64 - } 61 + type tokbuf = { mutable bytes : Stdlib.Bytes.t; mutable len : int } 65 62 66 63 let tokbuf_create n = { bytes = Stdlib.Bytes.create n; len = 0 } 67 - 68 64 let[@inline] tokbuf_clear t = t.len <- 0 69 65 70 66 let[@inline] tokbuf_ensure t need = 71 67 let cap = Stdlib.Bytes.length t.bytes in 72 - if t.len + need > cap then 68 + if t.len + need > cap then ( 73 69 let new_cap = max (cap * 2) (t.len + need) in 74 70 let b = Stdlib.Bytes.create new_cap in 75 71 Stdlib.Bytes.blit t.bytes 0 b 0 t.len; 76 - t.bytes <- b 72 + t.bytes <- b) 77 73 78 74 let[@inline] tokbuf_add_char t c = 79 75 tokbuf_ensure t 1;
+1 -2
lib/dune
··· 1 1 (library 2 2 (name json) 3 3 (public_name json) 4 - (modules json json_base) 5 - (private_modules json_base) 4 + (modules json core) 6 5 (libraries 7 6 (re_export loc)))
+48 -49
lib/json.ml
··· 3 3 SPDX-License-Identifier: ISC 4 4 ---------------------------------------------------------------------------*) 5 5 6 - module Fmt = Json_base.Fmt 6 + module Fmt = Core.Fmt 7 7 8 8 type 'a fmt = 'a Fmt.t 9 9 ··· 18 18 type 'a node = 'a * Meta.t 19 19 20 20 module Path = Loc.Path 21 - module Sort = Json_base.Sort 21 + module Sort = Core.Sort 22 22 23 23 exception Error = Loc.Error 24 24 ··· 137 137 module Repr = struct 138 138 (* See the .mli for documentation *) 139 139 module String_map = Map.Make (String) 140 - module Type = Json_base.Type 140 + module Type = Core.Type 141 141 142 142 type ('ret, 'f) dec_fun = 143 143 | Dec_fun : 'f -> ('ret, 'f) dec_fun ··· 157 157 | Number : (float, 'a) base_map -> 'a t 158 158 | String : (string, 'a) base_map -> 'a t 159 159 | Array : ('a, 'elt, 'builder) array_map -> 'a t 160 - | Object : ('o, 'o) object_map -> 'o t 160 + | Object : ('o, 'o) object_map -> 'o codec 161 161 | Any : 'a any_map -> 'a t 162 - | Map : ('a, 'b) map -> 'b t 162 + | Map : ('a, 'b) map -> 'b codec 163 163 | Rec : 'a t Lazy.t -> 'a t 164 - | Ignore : unit t 164 + | Ignore : unit codec 165 165 (** Skip-parse any JSON value without materialising its contents. The 166 166 bytesrw decoder dispatches to [skip_json_value], which advances past 167 167 the value at the byte level (balancing brackets, skipping string ··· 506 506 507 507 (* Types *) 508 508 509 - type 'a t = 'a Repr.t 509 + type 'a codec = 'a Repr.t 510 510 511 511 let kinded_sort = Repr.kinded_sort 512 512 let kind = Repr.kind ··· 716 716 let kind = "uint8" in 717 717 let dec meta v = 718 718 check_finite_number meta ~kind v; 719 - if Json_base.Number.in_exact_uint8_range v then Int.of_float v 719 + if Core.Number.in_exact_uint8_range v then Int.of_float v 720 720 else Error.number_range meta ~kind v 721 721 in 722 722 let enc v = 723 - if Json_base.Number.int_is_uint8 v then Int.to_float v 723 + if Core.Number.int_is_uint8 v then Int.to_float v 724 724 else Error.integer_range Meta.none ~kind v 725 725 in 726 726 Base.number (Base.map ~kind ~dec ~enc ()) ··· 729 729 let kind = "uint16" in 730 730 let dec meta v = 731 731 check_finite_number meta ~kind v; 732 - if Json_base.Number.in_exact_uint16_range v then Int.of_float v 732 + if Core.Number.in_exact_uint16_range v then Int.of_float v 733 733 else Error.number_range meta ~kind v 734 734 in 735 735 let enc v = 736 - if Json_base.Number.int_is_uint16 v then Int.to_float v 736 + if Core.Number.int_is_uint16 v then Int.to_float v 737 737 else Error.integer_range Meta.none ~kind v 738 738 in 739 739 Base.number (Base.map ~kind ~dec ~enc ()) ··· 742 742 let kind = "int8" in 743 743 let dec meta v = 744 744 check_finite_number meta ~kind v; 745 - if Json_base.Number.in_exact_int8_range v then Int.of_float v 745 + if Core.Number.in_exact_int8_range v then Int.of_float v 746 746 else Error.number_range meta ~kind v 747 747 in 748 748 let enc v = 749 - if Json_base.Number.int_is_int8 v then Int.to_float v 749 + if Core.Number.int_is_int8 v then Int.to_float v 750 750 else Error.integer_range Meta.none ~kind v 751 751 in 752 752 Base.number (Base.map ~kind ~dec ~enc ()) ··· 755 755 let kind = "int16" in 756 756 let dec meta v = 757 757 check_finite_number meta ~kind v; 758 - if Json_base.Number.in_exact_int16_range v then Int.of_float v 758 + if Core.Number.in_exact_int16_range v then Int.of_float v 759 759 else Error.number_range meta ~kind v 760 760 in 761 761 let enc v = 762 - if Json_base.Number.int_is_int16 v then Int.to_float v 762 + if Core.Number.int_is_int16 v then Int.to_float v 763 763 else Error.integer_range Meta.none ~kind v 764 764 in 765 765 Base.number (Base.map ~kind ~dec ~enc ()) ··· 768 768 let kind = "int32" in 769 769 let dec meta v = 770 770 check_finite_number meta ~kind v; 771 - if Json_base.Number.in_exact_int32_range v then Int32.of_float v 771 + if Core.Number.in_exact_int32_range v then Int32.of_float v 772 772 else Error.number_range meta ~kind v 773 773 in 774 774 let enc = ··· 791 791 range on encoding. *) 792 792 let kind = "int64" in 793 793 let dec meta v = 794 - if Json_base.Number.in_exact_int64_range v then Int64.of_float v 794 + if Core.Number.in_exact_int64_range v then Int64.of_float v 795 795 else Error.number_range meta ~kind v 796 796 in 797 797 Base.number (Base.map ~kind ~dec ~enc:Int64.to_float ()) ··· 799 799 let int64 = 800 800 let dec_number = int64_number and dec_string = int64_as_string in 801 801 let enc v = 802 - if Json_base.Number.can_store_exact_int64 v then int64_number 802 + if Core.Number.can_store_exact_int64 v then int64_number 803 803 else int64_as_string 804 804 in 805 805 any ~kind:"int64" ~dec_number ~dec_string ~enc () ··· 818 818 encoding. *) 819 819 let kind = "OCaml int" in 820 820 let dec meta v = 821 - if Json_base.Number.in_exact_int_range v then Int.of_float v 821 + if Core.Number.in_exact_int_range v then Int.of_float v 822 822 else Error.number_range meta ~kind v 823 823 in 824 824 Base.number (Base.map ~kind ~dec ~enc:Int.to_float ()) 825 825 826 826 let int = 827 827 let enc v = 828 - if Json_base.Number.can_store_exact_int v then int_number else int_as_string 828 + if Core.Number.can_store_exact_int v then int_number else int_as_string 829 829 in 830 830 let dec_number = int_number and dec_string = int_as_string in 831 831 any ~kind:"OCaml int" ~dec_number ~dec_string ~enc () ··· 875 875 let binary_string = 876 876 let kind = "hex" in 877 877 let kind' = Sort.kinded ~kind String in 878 - let dec = Base.dec_result ~kind:kind' Json_base.binary_string_of_hex in 879 - let enc = Base.enc Json_base.binary_string_to_hex in 878 + let dec = Base.dec_result ~kind:kind' Core.binary_string_of_hex in 879 + let enc = Base.enc Core.binary_string_to_hex in 880 880 Base.string (Base.map ~kind ~dec ~enc ()) 881 881 882 882 (* Arrays and tuples *) ··· 940 940 let enc = { enc = list_enc } in 941 941 map ?kind ?doc ~dec_empty ?dec_skip ~dec_add ~dec_finish ~enc elt 942 942 943 - type 'a array_builder = 'a Json_base.Rarray.t 943 + type 'a array_builder = 'a Core.Rarray.t 944 944 945 945 let array_enc f acc a = 946 946 let acc = ref acc in ··· 950 950 !acc 951 951 952 952 let array_map ?kind ?doc ?dec_skip elt = 953 - let dec_empty () = Json_base.Rarray.empty () in 954 - let dec_add _i v a = Json_base.Rarray.add_last v a in 955 - let dec_finish _meta _len a = Json_base.Rarray.to_array a in 953 + let dec_empty () = Core.Rarray.empty () in 954 + let dec_add _i v a = Core.Rarray.add_last v a in 955 + let dec_finish _meta _len a = Core.Rarray.to_array a in 956 956 let enc = { enc = array_enc } in 957 957 map ?kind ?doc ~dec_empty ?dec_skip ~dec_add ~dec_finish ~enc elt 958 958 959 - type ('a, 'b, 'c) bigarray_builder = ('a, 'b, 'c) Json_base.Rbigarray1.t 959 + type ('a, 'b, 'c) bigarray_builder = ('a, 'b, 'c) Core.Rbigarray1.t 960 960 961 961 let bigarray_map ?kind ?doc ?dec_skip k l elt = 962 - let dec_empty _meta = Json_base.Rbigarray1.empty k l in 963 - let dec_add _i v a = Json_base.Rbigarray1.add_last v a in 964 - let dec_finish _meta _len a = Json_base.Rbigarray1.to_bigarray a in 962 + let dec_empty _meta = Core.Rbigarray1.empty k l in 963 + let dec_add _i v a = Core.Rbigarray1.add_last v a in 964 + let dec_finish _meta _len a = Core.Rbigarray1.to_bigarray a in 965 965 let enc f acc a = 966 966 let acc = ref acc in 967 967 for i = 0 to Bigarray.Array1.dim a - 1 do ··· 1102 1102 Repr.Array (Array.map ~kind ?doc ~dec_empty ~dec_add ~dec_finish ~enc t) 1103 1103 1104 1104 let tn ?(kind = "") ?doc ~n elt = 1105 - let dec_empty () = Json_base.Rarray.empty () in 1106 - let dec_add _i v a = Json_base.Rarray.add_last v a in 1105 + let dec_empty () = Core.Rarray.empty () in 1106 + let dec_add _i v a = Core.Rarray.add_last v a in 1107 1107 let dec_finish meta _len a = 1108 - let len = Json_base.Rarray.length a in 1108 + let len = Core.Rarray.length a in 1109 1109 if len <> n then error_tuple_size meta kind ~exp:n len 1110 - else Json_base.Rarray.to_array a 1110 + else Core.Rarray.to_array a 1111 1111 in 1112 1112 let enc = { Array.enc = Array.array_enc } in 1113 1113 Repr.Array (Array.map ~kind ?doc ~dec_empty ~dec_add ~dec_finish ~enc elt) ··· 1361 1361 decoder can skip-parse the value (no token buffers, no float parsing, 1362 1362 no DOM allocation). *) 1363 1363 1364 - let ignore : unit t = Repr.Ignore 1364 + let ignore : unit codec = Repr.Ignore 1365 1365 1366 1366 let zero = 1367 1367 let kind = "zero" in ··· 1389 1389 type mem = name * json 1390 1390 and object' = mem list 1391 1391 1392 - and json = 1392 + and t = 1393 1393 | Null of unit node 1394 1394 | Bool of bool node 1395 1395 | Number of float node 1396 1396 | String of string node 1397 - | Array of json list node 1397 + | Array of t list node 1398 1398 | Object of object' node 1399 1399 1400 1400 let pp_null = Fmt.json_null ··· 1445 1445 1446 1446 (* Generic JSON *) 1447 1447 1448 - module Json = struct 1448 + module Value = struct 1449 1449 type 'a cons = ?meta:Meta.t -> 'a -> json 1450 1450 type t = json 1451 1451 ··· 1476 1476 | Array _ -> Sort.Array 1477 1477 | Object _ -> Sort.Object 1478 1478 1479 - let rec compare (j0 : json) (j1 : json) = 1479 + let rec compare (j0 : t) (j1 : t) = 1480 1480 match (j0, j1) with 1481 1481 | Null ((), _), Null ((), _) -> 0 1482 1482 | Null _, _ -> -1 ··· 1527 1527 let int64_as_string ?(meta = Meta.none) v = String (Int64.to_string v, meta) 1528 1528 1529 1529 let int64 ?(meta = Meta.none) v = 1530 - if Json_base.Number.can_store_exact_int64 v then 1531 - Number (Int64.to_float v, meta) 1530 + if Core.Number.can_store_exact_int64 v then Number (Int64.to_float v, meta) 1532 1531 else String (Int64.to_string v, meta) 1533 1532 1534 1533 let int_as_string ?(meta = Meta.none) i = String (Int.to_string i, meta) 1535 1534 1536 1535 let int ?(meta = Meta.none) v = 1537 - if Json_base.Number.can_store_exact_int v then Number (Int.to_float v, meta) 1536 + if Core.Number.can_store_exact_int v then Number (Int.to_float v, meta) 1538 1537 else String (Int.to_string v, meta) 1539 1538 1540 1539 (* Strings *) ··· 1593 1592 1594 1593 (* Decoding *) 1595 1594 1596 - let rec decode : type a. a Repr.t -> json -> a = 1595 + let rec decode : type a. a Repr.t -> t -> a = 1597 1596 fun t j -> 1598 1597 match t with 1599 1598 | Null map -> ( ··· 1621 1620 | Ignore -> () 1622 1621 1623 1622 and decode_array : type a elt b. 1624 - (a, elt, b) array_map -> Meta.t -> json list -> a = 1623 + (a, elt, b) array_map -> Meta.t -> t list -> a = 1625 1624 fun map meta vs -> 1626 1625 let rec next (map : (a, elt, b) array_map) meta b i = function 1627 1626 | [] -> map.dec_finish meta i b ··· 1767 1766 decode_object_cases map meta umems cases mem_miss mem_decs dict 1768 1767 delay mems) 1769 1768 1770 - and decode_any : type a. a Repr.t -> a any_map -> json -> a = 1769 + and decode_any : type a. a Repr.t -> a any_map -> t -> a = 1771 1770 fun t map j -> 1772 1771 let dec t map j = 1773 1772 match map with Some t -> decode t j | None -> error_type t j ··· 1981 1980 1982 1981 (* Queries and updates *) 1983 1982 1984 - (* val app : ('a -> 'b) t -> 'a t -> 'b t 1985 - val product : 'a t -> 'b t -> ('a * 'b) t 1986 - val bind : 'a t -> ('a -> 'b t) -> 'b t 1987 - val map : ('a -> 'b) -> 'a t -> 'b t *) 1983 + (* val app : ('a -> 'b) t -> 'a t -> 'b codec 1984 + val product : 'a t -> 'b codec -> ('a * 'b) t 1985 + val bind : 'a t -> ('a -> 'b t) -> 'b codec 1986 + val map : ('a -> 'b) -> 'a t -> 'b codec *) 1988 1987 1989 1988 let const t v = 1990 1989 let const _ = v in
+165 -163
lib/json.mli
··· 169 169 170 170 (** {1:types Types} *) 171 171 172 - type 'a t 172 + type 'a codec 173 173 (** The type for JSON types. 174 174 175 175 A value of this type represents a subset of JSON values mapped to a subset 176 176 of values of type ['a] and vice versa. *) 177 177 178 - val kinded_sort : 'a t -> string 178 + val kinded_sort : 'a codec -> string 179 179 (** [kinded_sort t] is a human readable string describing the JSON values typed 180 180 by [t]. This combines the kind of the map with the {{!Sort}sort}(s) of JSON 181 181 value mapped by [t]. For example if [t] is an object map and the kind 182 182 specified for the {{!Object.val-map}map} is ["T"] then this is ["T object"], 183 183 if the kind is empty this is simply ["object"]. See also {!Sort.kinded}. *) 184 184 185 - val kind : 'a t -> string 185 + val kind : 'a codec -> string 186 186 (** [kind t] is the [kind] of the underlying map. If the kind is an empty string 187 187 this falls back to mention the {{!Sort}sort}. For example if [t] is an 188 188 object map and the kind specified for the {{!Object.val-map}map} is ["T"] 189 189 then this is ["T"], if the kind is empty then this is ["object"]. See also 190 190 {!Sort.or_kind}. *) 191 191 192 - val doc : 'a t -> string 192 + val doc : 'a codec -> string 193 193 (** [doc t] is a documentation string for the JSON values typed by [t]. *) 194 194 195 - val with_doc : ?kind:string -> ?doc:string -> 'a t -> 'a t 195 + val with_doc : ?kind:string -> ?doc:string -> 'a codec -> 'a codec 196 196 (** [with_doc ?kind ?doc t] is [t] with its {!doc} or {!kind} updated to the 197 197 corresponding values if specified. *) 198 198 ··· 240 240 241 241 (** {2:types JSON types} *) 242 242 243 - val null : (unit, 'a) map -> 'a t 243 + val null : (unit, 'a) map -> 'a codec 244 244 (** [null map] maps with [map] JSON nulls represented by [()] to values of 245 245 type ['a]. See also {!Json.null}. *) 246 246 247 - val bool : (bool, 'a) map -> 'a t 247 + val bool : (bool, 'a) map -> 'a codec 248 248 (** [bool map] maps with [map] JSON booleans represented by [bool] values to 249 249 values of type ['a]. See also {!Json.bool}. *) 250 250 251 - val number : (float, 'a) map -> 'a t 251 + val number : (float, 'a) map -> 'a codec 252 252 (** [number map] maps with [map] JSON nulls or numbers represented by [float] 253 253 values to values of type ['a]. The [float] representation decodes JSON 254 254 nulls to {!Float.nan} and lossily encodes any ··· 256 256 ({{!page-cookbook.non_finite_numbers}explanation}). See also 257 257 {!Json.number}. *) 258 258 259 - val string : (string, 'a) map -> 'a t 259 + val string : (string, 'a) map -> 'a codec 260 260 (** [string map] maps with [map] {e unescaped} JSON strings represented by 261 261 UTF-8 encoded [string] values to values of type ['a]. See also 262 262 {!Json.string}. *) ··· 296 296 297 297 Read the {{!page-cookbook.dealing_with_null}cookbook} on [null]s. *) 298 298 299 - val null : ?kind:string -> ?doc:string -> 'a -> 'a t 299 + val null : ?kind:string -> ?doc:string -> 'a -> 'a codec 300 300 (** [null v] maps JSON nulls to [v]. On encodes any value of type ['a] is 301 301 encoded by null. [doc] and [kind] are given to the underlying 302 302 {!Base.type-map}. See also {!Base.null}. *) 303 303 304 - val none : 'a option t 304 + val none : 'a option codec 305 305 (** [none] maps JSON nulls to [None]. *) 306 306 307 - val some : 'a t -> 'a option t 307 + val some : 'a codec -> 'a option codec 308 308 (** [some t] maps JSON like [t] does but wraps results in [Some]. Encoding fails 309 309 if the value is [None]. *) 310 310 311 - val option : ?kind:string -> ?doc:string -> 'a t -> 'a option t 311 + val option : ?kind:string -> ?doc:string -> 'a codec -> 'a option codec 312 312 (** [option t] maps JSON nulls to [None] and other values by [t]. [doc] and 313 313 [kind] are given to the underlying {!val-any} map. *) 314 314 315 315 (** {2:booleans Booleans} *) 316 316 317 - val bool : bool t 317 + val bool : bool codec 318 318 (** [bool] maps JSON booleans to [bool] values. See also {!Base.bool}. *) 319 319 320 320 (** {2:numbers Numbers} ··· 322 322 Read the {{!page-cookbook.dealing_with_numbers}cookbook} on JSON numbers and 323 323 their many pitfalls. *) 324 324 325 - val number : float t 325 + val number : float codec 326 326 (** [number] maps JSON nulls or numbers to [float] values. On decodes JSON null 327 327 is mapped to {!Float.nan}. On encodes any {{!Float.is_finite}non-finite} 328 328 float is lossily mapped to JSON null 329 329 ({{!page-cookbook.non_finite_numbers}explanation}). See also {!Base.number}, 330 330 {!any_float} and the integer combinators below. *) 331 331 332 - val any_float : float t 332 + val any_float : float codec 333 333 (** [any_float] is a lossless representation for IEEE 754 doubles. It maps 334 334 {{!Float.is_finite}non-finite} floats by the JSON strings defined by 335 335 {!Float.to_string}. This contrasts with {!val-number} which maps them to ··· 342 342 agreed on such an encoding. To maximize interoperability you should use the 343 343 lossy {!val-number} map. *) 344 344 345 - val float_as_hex_string : float t 345 + val float_as_hex_string : float codec 346 346 (** [float_as_hex_string] maps JSON strings made of IEEE 754 doubles in hex 347 347 notation to float values. On encodes strings this uses the ["%h"] format 348 348 string. On decodes it accepts anything sucessfully decoded by 349 349 {!Float.of_string_opt}. *) 350 350 351 - val uint8 : int t 351 + val uint8 : int codec 352 352 (** [uint8] maps JSON numbers to unsigned 8-bit integers. JSON numbers are 353 353 sucessfully decoded if after truncation they can be represented on the 354 354 \[0;255\] range. Encoding errors if the integer is out of range.*) 355 355 356 - val uint16 : int t 356 + val uint16 : int codec 357 357 (** [uint16] maps JSON numbers to unsigned 16-bit integers. JSON numbers are 358 358 sucessfully decoded if after truncation they can be represented on the 359 359 \[0;65535\] range. Encoding errors if the integer is out of range.*) 360 360 361 - val int8 : int t 361 + val int8 : int codec 362 362 (** [int8] maps JSON numbers to 8-bit integers. JSON numbers are sucessfully 363 363 decoded if after truncation they can be represented on the \[-128;127\] 364 364 range. Encoding errors if the integer is out of range.*) 365 365 366 - val int16 : int t 366 + val int16 : int codec 367 367 (** [int16] maps JSON numbers to 16-bit integers. JSON numbers are sucessfully 368 368 decoded if after truncation they can be represented on the \[-32768;32767\] 369 369 range. Encoding errors if the integer is out of range. *) 370 370 371 - val int32 : int32 t 371 + val int32 : int32 codec 372 372 (** [int32] maps JSON numbers to 32-bit integers. JSON numbers are sucessfully 373 373 decoded if after truncation they can be represented on the [int32] range, 374 374 otherwise the decoder errors. *) 375 375 376 - val int64 : int64 t 376 + val int64 : int64 codec 377 377 (** [int] maps truncated JSON numbers or JSON strings to 64-bit integers. 378 378 - JSON numbers are sucessfully decoded if after truncation they can be 379 379 represented on the [int64] range, otherwise the decoder errors. [int64] ··· 384 384 [int] values are encoded as JSON strings with {!Int.to_string} when the 385 385 integer is outside the \[-2{^ 53};2{^ 53}\] range *) 386 386 387 - val int64_as_string : int64 t 387 + val int64_as_string : int64 codec 388 388 (** [int64_as_string] maps JSON strings to 64-bit integers. On decodes this uses 389 389 {!Int64.of_string_opt} which allows binary, octal, decimal and hex syntaxes 390 390 and errors on overflow and syntax errors. On encodes uses 391 391 {!Int64.to_string}. *) 392 392 393 - val int : int t 393 + val int : int codec 394 394 (** [int] maps truncated JSON numbers or JSON strings to [int] values. 395 395 - JSON numbers are sucessfully decoded if after truncation they can be 396 396 represented on the [int] range, otherwise the decoder errors. [int] values ··· 404 404 {b Warning.} The behaviour of this function is platform dependent, it 405 405 depends on the value of {!Sys.int_size}. *) 406 406 407 - val int_as_string : int t 407 + val int_as_string : int codec 408 408 (** [int_as_string] maps JSON strings to [int] values. On decodes this uses 409 409 {!int_of_string_opt} which allows binary, octal, decimal and hex syntaxes 410 410 and errors on overflow and syntax errors. On encodes uses {!Int.to_string}. ··· 417 417 Read the {{!page-cookbook.transform_strings}cookbook} on transforming 418 418 strings. *) 419 419 420 - val string : string t 420 + val string : string codec 421 421 (** [string] maps unescaped JSON strings to UTF-8 encoded [string] values. See 422 422 also {!Base.string}. 423 423 ··· 429 429 ?doc:string -> 430 430 ?enc:('a -> string) -> 431 431 (string -> ('a, string) result) -> 432 - 'a t 432 + 'a codec 433 433 (** [of_of_string of_string] maps JSON string with a {{!Base.type-map}base map} 434 434 using [of_string] for decoding and [enc] for encoding. See the 435 435 {{!page-cookbook.transform_strings}cookbook}. *) ··· 439 439 ?kind:string -> 440 440 ?doc:string -> 441 441 (string * 'a) list -> 442 - 'a t 442 + 'a codec 443 443 (** [enum assoc] maps JSON strings member of the [assoc] list to the 444 444 corresponding OCaml value and vice versa in log(n). [cmp] is used to compare 445 445 the OCaml values, it defaults to {!Stdlib.compare}. Decoding and encoding 446 446 errors on strings or values not part of [assoc] *) 447 447 448 - val binary_string : string t 448 + val binary_string : string codec 449 449 (** [binary_string] maps JSON strings made of an even number of hexdecimal 450 450 US-ASCII upper or lower case digits to the corresponding byte sequence. On 451 451 encoding uses only lower case hexadecimal digits to encode the byte ··· 479 479 ?dec_finish:(Meta.t -> int -> 'builder -> 'array) -> 480 480 ?enc:('array, 'elt) enc -> 481 481 ?enc_meta:('array -> Meta.t) -> 482 - 'elt t -> 482 + 'elt codec -> 483 483 ('array, 'elt, 'builder) map 484 484 (** [map elt] maps JSON arrays of type ['elt] to arrays of type ['array] built 485 485 with type ['builder]. ··· 508 508 ?kind:string -> 509 509 ?doc:string -> 510 510 ?dec_skip:(int -> 'a list -> bool) -> 511 - 'a t -> 511 + 'a codec -> 512 512 ('a list, 'a, 'a list) map 513 513 (** [list_map elt] maps JSON arrays with elements of type [elt] to [list] 514 514 values. See also {!Json.list}. *) ··· 520 520 ?kind:string -> 521 521 ?doc:string -> 522 522 ?dec_skip:(int -> 'a array_builder -> bool) -> 523 - 'a t -> 523 + 'a codec -> 524 524 ('a array, 'a, 'a array_builder) map 525 525 (** [array_map elt] maps JSON arrays with elements of type [elt] to [array] 526 526 values. See also {!Json.array}. *) ··· 534 534 ?dec_skip:(int -> ('a, 'b, 'c) bigarray_builder -> bool) -> 535 535 ('a, 'b) Bigarray.kind -> 536 536 'c Bigarray.layout -> 537 - 'a t -> 537 + 'a codec -> 538 538 (('a, 'b, 'c) Bigarray.Array1.t, 'a, ('a, 'b, 'c) bigarray_builder) map 539 539 (** [bigarray k l elt] maps JSON arrays with elements of type [elt] to 540 540 bigarray values of kind [k] and layout [l]. See also {!Json.bigarray}. *) 541 541 542 542 (** {1:types JSON types} *) 543 543 544 - val array : ('a, _, _) map -> 'a t 544 + val array : ('a, _, _) map -> 'a codec 545 545 (** [array map] maps with [map] JSON arrays to values of type ['a]. See the 546 546 the {{!section-arrays}array combinators}. *) 547 547 548 - val ignore : unit t 548 + val ignore : unit codec 549 549 (** [ignore] ignores JSON arrays on decoding and errors on encoding. *) 550 550 551 - val zero : unit t 551 + val zero : unit codec 552 552 (** [zero] ignores JSON arrays on decoding and encodes an empty array. *) 553 553 end 554 554 555 - val list : ?kind:string -> ?doc:string -> 'a t -> 'a list t 555 + val list : ?kind:string -> ?doc:string -> 'a codec -> 'a list codec 556 556 (** [list t] maps JSON arrays of type [t] to [list] values. See also 557 557 {!Array.list_map}. *) 558 558 559 - val array : ?kind:string -> ?doc:string -> 'a t -> 'a array t 559 + val array : ?kind:string -> ?doc:string -> 'a codec -> 'a array codec 560 560 (** [array t] maps JSON arrays of type [t] to [array] values. See also 561 561 {!Array.array_map}. *) 562 562 ··· 564 564 ?kind:string -> 565 565 ?doc:string -> 566 566 key:('a -> string) -> 567 - 'a t -> 568 - 'a Map.Make(String).t t 567 + 'a codec -> 568 + 'a Map.Make(String).t codec 569 569 (** [array_as_string_map ~key t] maps JSON array elements of type [t] to string 570 570 maps by indexing them with [key]. If two elements have the same [key] the 571 571 element with the greatest index takes over. Elements of the map are encoded ··· 575 575 ?kind:string -> 576 576 ?doc:string -> 577 577 ('a, 'b) Bigarray.kind -> 578 - 'a t -> 579 - ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t t 578 + 'a codec -> 579 + ('a, 'b, Bigarray.c_layout) Bigarray.Array1.t codec 580 580 (** [bigarray k t] maps JSON arrays of type [t] to [Bigarray.Array1.t] values. 581 581 See also {!Array.bigarray_map}. *) 582 582 ··· 585 585 ?doc:string -> 586 586 ?dec:('a -> 'a -> 't2) -> 587 587 ?enc:('t2 -> int -> 'a) -> 588 - 'a t -> 588 + 'a codec -> 589 589 't2 t 590 590 (** [t2 ?dec ?enc t] maps JSON arrays with exactly 2 elements of type [t] to 591 591 value of type ['t2]. Decodes error if there are more elements. [enc v i] ··· 596 596 ?doc:string -> 597 597 ?dec:('a -> 'a -> 'a -> 't3) -> 598 598 ?enc:('t3 -> int -> 'a) -> 599 - 'a t -> 599 + 'a codec -> 600 600 't3 t 601 601 (** [t3] is like {!t2} but for 3 elements. *) 602 602 ··· 605 605 ?doc:string -> 606 606 ?dec:('a -> 'a -> 'a -> 'a -> 't4) -> 607 607 ?enc:('t4 -> int -> 'a) -> 608 - 'a t -> 608 + 'a codec -> 609 609 't4 t 610 610 (** [t4] is like {!t2} but for 4 elements. *) 611 611 612 - val tn : ?kind:string -> ?doc:string -> n:int -> 'a t -> 'a array t 612 + val tn : ?kind:string -> ?doc:string -> n:int -> 'a codec -> 'a array codec 613 613 (** [tn ~n t] maps JSON arrays of exactly [n] elements of type [t] to [array] 614 614 values. This is {!val-array} limited by [n]. *) 615 615 ··· 653 653 ('o, 'a) map 654 654 (** [enc_only ()] is like {!val-map'} but can only be used for encoding. *) 655 655 656 - val finish : ('o, 'o) map -> 'o t 656 + val finish : ('o, 'o) map -> 'o codec 657 657 (** [finish map] is a JSON type for objects mapped by [map]. Raises 658 658 [Invalid_argument] if [map] describes a member name more than once. *) 659 659 ··· 676 676 ?enc:('o -> 'a) -> 677 677 ?enc_omit:('a -> bool) -> 678 678 string -> 679 - 'a t -> 679 + 'a codec -> 680 680 ('o, 'a) map 681 681 (** See {!Json.Object.mem}. *) 682 682 ··· 692 692 ?enc:('o -> 'a) -> 693 693 ?enc_omit:('a -> bool) -> 694 694 string -> 695 - 'a t -> 695 + 'a codec -> 696 696 ('o, 'a -> 'b) map -> 697 697 ('o, 'b) map 698 698 (** [mem name t map] is a member named [name] of type [t] for an object of ··· 714 714 ?doc:string -> 715 715 ?enc:('o -> 'a option) -> 716 716 string -> 717 - 'a t -> 717 + 'a codec -> 718 718 ('o, 'a option -> 'b) map -> 719 719 ('o, 'b) map 720 720 (** [opt_mem name t map] is: ··· 737 737 module Case : sig 738 738 (** {1:maps Maps} *) 739 739 740 - type 'a codec := 'a t 740 + type 'a codec := 'a codec 741 741 742 742 type ('cases, 'case, 'tag) map 743 743 (** The type for mapping a case object represented by ['case] belonging to a ··· 828 828 module Mems : sig 829 829 (** {1:maps Maps} *) 830 830 831 - type 'a codec := 'a t 831 + type 'a codec := 'a codec 832 832 833 833 type ('mems, 'a) enc = { 834 834 enc : ··· 903 903 (** {1:types JSON types} *) 904 904 905 905 val as_string_map : 906 - ?kind:string -> ?doc:string -> 'a t -> 'a Stdlib.Map.Make(String).t t 906 + ?kind:string -> ?doc:string -> 'a codec -> 'a Stdlib.Map.Make(String).t codec 907 907 (** [as_string_map t] maps object to key-value maps of type [t]. See also 908 908 {!Mems.string_map} and {!Json.json_mems}. *) 909 909 910 - val zero : unit t 910 + val zero : unit codec 911 911 (** [zero] ignores JSON objects on decoding and encodes an empty object. *) 912 912 end 913 913 ··· 916 916 val any : 917 917 ?kind:string -> 918 918 ?doc:string -> 919 - ?dec_null:'a t -> 920 - ?dec_bool:'a t -> 921 - ?dec_number:'a t -> 922 - ?dec_string:'a t -> 923 - ?dec_array:'a t -> 924 - ?dec_object:'a t -> 925 - ?enc:('a -> 'a t) -> 919 + ?dec_null:'a codec -> 920 + ?dec_bool:'a codec -> 921 + ?dec_number:'a codec -> 922 + ?dec_string:'a codec -> 923 + ?dec_array:'a codec -> 924 + ?dec_object:'a codec -> 925 + ?enc:('a -> 'a codec) -> 926 926 unit -> 927 - 'a t 927 + 'a codec 928 928 (** [any ()] maps subsets of JSON value of different sorts to values of type 929 929 ['a]. The unspecified cases are not part of the subset and error on 930 930 decoding. [enc] selects the type to use on encoding and errors if omitted. ··· 938 938 ?doc:string -> 939 939 ?dec:('a -> 'b) -> 940 940 ?enc:('b -> 'a) -> 941 - 'a t -> 942 - 'b t 941 + 'a codec -> 942 + 'b codec 943 943 (** [map t] changes the type of [t] from ['a] to ['b]. 944 944 - [kind] names the entities represented by the type and [doc] documents 945 945 them, both default to [""]. ··· 955 955 ?doc:string -> 956 956 ?dec:('a -> unit) -> 957 957 ?enc:('a -> unit) -> 958 - 'a t -> 959 - 'a t 958 + 'a codec -> 959 + 'a codec 960 960 (** [iter ?enc dec t] applies [dec] on decoding and [enc] on encoding but 961 961 otherwise behaves like [t] does. Typically [dec] can be used to further 962 962 assert the shape of the decoded value and {!Error.msgf} if it hasn't the 963 963 right shape. [iter] can also be used as a tracing facility for debugging. *) 964 964 965 - val rec' : 'a t Lazy.t -> 'a t 965 + val rec' : 'a codec Lazy.t -> 'a codec 966 966 (** [rec'] maps recursive JSON values. See the 967 967 {{!page-cookbook.recursion} cookbook}. *) 968 968 969 969 (** {1:ignoring Ignoring} *) 970 970 971 - val ignore : unit t 971 + val ignore : unit codec 972 972 (** [ignore] lossily maps all JSON values to [()] on decoding and errors on 973 973 encoding. See also {!const}. 974 974 ··· 976 976 advances past the value without materialising strings, numbers or nested 977 977 DOM. The fast path matches {{:https://simdjson.org}simdjson}'s On-Demand 978 978 semantics: it enforces the structural contract (bracket nesting, quote 979 - matching, well-formed literal tokens) but does {b not} validate the 980 - content of ignored values. Concretely: 979 + matching, well-formed literal tokens) but does {b not} validate the content 980 + of ignored values. Concretely: 981 981 982 982 - Malformed number shapes like [1..2], [+5], [1eE2] pass through. 983 983 - Unrecognised escape characters ([\\z]) and short [\\u] sequences pass 984 984 through. 985 - - UTF-8 in string content is {b not} validated while skipping 986 - (multibyte sequences are skipped as-is). 985 + - UTF-8 in string content is {b not} validated while skipping (multibyte 986 + sequences are skipped as-is). 987 987 988 - Callers needing strict content validation should decode with 989 - {!json} and discard the result rather than reaching for [ignore]. *) 988 + Callers needing strict content validation should decode with {!json} and 989 + discard the result rather than reaching for [ignore]. *) 990 990 991 - val zero : unit t 991 + val zero : unit codec 992 992 (** [zero] lossily maps all JSON values to [()] on decoding and encodes JSON 993 993 nulls. *) 994 994 995 - val todo : ?kind:string -> ?doc:string -> ?dec_stub:'a -> unit -> 'a t 995 + val todo : ?kind:string -> ?doc:string -> ?dec_stub:'a -> unit -> 'a codec 996 996 (** [todo ?dec_stub ()] maps all JSON values to [dec_stub] if specified (errors 997 997 otherwise) and errors on encoding. *) 998 998 ··· 1008 1008 (** The type for generic JSON objects. *) 1009 1009 1010 1010 (** The type for generic JSON values. *) 1011 - and json = 1011 + and t = 1012 1012 | Null of unit node 1013 1013 | Bool of bool node 1014 1014 | Number of float node 1015 1015 (** Encoders must use [Null] if float is {{!Float.is_finite}not finite}. 1016 1016 *) 1017 1017 | String of string node 1018 - | Array of json list node 1018 + | Array of t list node 1019 1019 | Object of object' node (** *) 1020 1020 1021 1021 (** Generic JSON values. *) 1022 - module Json : sig 1022 + module Value : sig 1023 1023 (** {1:json JSON values} *) 1024 - 1025 - type 'a codec := 'a t 1026 1024 1027 1025 type 'a cons = ?meta:Meta.t -> 'a -> json 1028 1026 (** The type for constructing JSON values from an OCaml value of type ['a]. ··· 1031 1029 type t = json 1032 1030 (** See {!Json.val-json}. *) 1033 1031 1034 - val meta : json -> Meta.t 1032 + val meta : t -> Meta.t 1035 1033 (** [meta v] is the metadata of value [v]. *) 1036 1034 1037 - val set_meta : Meta.t -> json -> json 1035 + val set_meta : Meta.t -> t -> json 1038 1036 (** [set_meta m v] replaces [v]'s meta with [m]. *) 1039 1037 1040 - val copy_layout : json -> dst:json -> json 1038 + val copy_layout : t -> dst:json -> json 1041 1039 (** [copy_layout src ~dst] copies the layout of [src] and sets it on [dst] 1042 1040 using {!Meta.copy_ws}. *) 1043 1041 1044 - val sort : json -> Sort.t 1042 + val sort : t -> Sort.t 1045 1043 (** [sort v] is the sort of value [v]. *) 1046 1044 1047 - val zero : json cons 1045 + val zero : t cons 1048 1046 (** [zero j] is a stub value of the sort value of [j]. The stub value is the 1049 1047 “natural” zero: null, false, 0, empty string, empty array, empty object. 1050 1048 *) 1051 1049 1052 - val equal : json -> json -> bool 1050 + val equal : t -> t -> bool 1053 1051 (** [equal j0 j1] is {!compare}[ j0 j1 = 0]. *) 1054 1052 1055 - val compare : json -> json -> int 1053 + val compare : t -> t -> int 1056 1054 (** [compare j0 j1] is a total order on JSON values: 1057 1055 - Floating point values are compared with {!Float.compare}, this means NaN 1058 1056 values are equal. ··· 1110 1108 1111 1109 (** {2:arrays Arrays} *) 1112 1110 1113 - val list : json list cons 1111 + val list : t list cons 1114 1112 (** [list l] is [Array (l, meta)]. *) 1115 1113 1116 - val array : json array cons 1114 + val array : t array cons 1117 1115 (** [array l] is [Array (Array.to_list a, meta)]. See also {!list}. *) 1118 1116 1119 1117 (** {2:objects Objects} *) ··· 1121 1119 val name : ?meta:Meta.t -> string -> name 1122 1120 (** [name ?meta n] is [(n, meta)]. [meta] defaults to {!Meta.none}. *) 1123 1121 1124 - val mem : name -> json -> mem 1122 + val mem : name -> t -> mem 1125 1123 (** [mem n v] is [(n, v)]. [meta] defaults to {!Meta.none}. *) 1126 1124 1127 1125 val object' : object' cons ··· 1141 1139 1142 1140 (** {1:decode Decode} *) 1143 1141 1144 - val decode : 'a codec -> json -> ('a, string) result 1142 + val decode : 'a codec -> t -> ('a, string) result 1145 1143 (** [decode t j] decodes a value from the generic JSON [j] according to type 1146 1144 [t]. *) 1147 1145 1148 - val decode' : 'a codec -> json -> ('a, Error.t) result 1146 + val decode' : 'a codec -> t -> ('a, Error.t) result 1149 1147 (** [decode'] is like {!val-decode} but preserves the error structure. *) 1150 1148 1151 1149 (** {1:encode Encode} *) ··· 1159 1157 1160 1158 (** {1:recode Recode} *) 1161 1159 1162 - val recode : 'a codec -> json -> (json, string) result 1160 + val recode : 'a codec -> t -> (json, string) result 1163 1161 (** [recode t v] decodes [v] with [t] and encodes it with [t]. *) 1164 1162 1165 - val recode' : 'a codec -> json -> (json, Error.t) result 1163 + val recode' : 'a codec -> t -> (json, Error.t) result 1166 1164 (** [recode'] is like {!val-recode} but preserves the error structure. *) 1167 1165 1168 - val update : 'a codec -> json -> json 1166 + val update : 'a codec -> t -> json 1169 1167 (** [update] is like {!val-recode} but raises {!Json.exception-Error}. *) 1170 1168 1171 1169 (** {1:errors Errors} *) 1172 1170 1173 - val error_sort : exp:Sort.t -> json -> 'a 1171 + val error_sort : exp:Sort.t -> t -> 'a 1174 1172 (** [error_sort ~exp fnd] errors when sort [exp] was expected but generic JSON 1175 1173 [fnd] was found. *) 1176 1174 1177 - val error_type : 'a codec -> json -> 'a 1175 + val error_type : 'a codec -> t -> 'a 1178 1176 (** [error_type t fnd] errors when the type expected by [t] does not match 1179 1177 [fnd]. *) 1180 1178 end 1181 1179 1182 - val json : json t 1180 + val json : t codec 1183 1181 (** [json] maps any JSON value to its generic representation. *) 1184 1182 1185 - val json_null : json t 1183 + val json_null : t codec 1186 1184 (** [json_null] maps JSON nulls to their generic representation. *) 1187 1185 1188 - val json_bool : json t 1186 + val json_bool : t codec 1189 1187 (** [json_bool] maps JSON booleans to their generic representation. *) 1190 1188 1191 - val json_number : json t 1189 + val json_number : t codec 1192 1190 (** [json_number] maps JSON nulls or numbers 1193 1191 ({{!page-cookbook.non_finite_numbers}explanation}) to their generic 1194 1192 representation. *) 1195 1193 1196 - val json_string : json t 1194 + val json_string : t codec 1197 1195 (** [json_string] represents JSON strings by their generic representation. *) 1198 1196 1199 - val json_array : json t 1197 + val json_array : t codec 1200 1198 (** [json_array] represents JSON arrays by their generic representation. *) 1201 1199 1202 - val json_object : json t 1200 + val json_object : t codec 1203 1201 (** [json_object] represents JSON objects by their generic representation. *) 1204 1202 1205 1203 val json_mems : (json, json, mem list) Object.Mems.map ··· 1213 1211 without having to fully model it (see the update example in the 1214 1212 {{!page-index.quick_start}quick start}). *) 1215 1213 1216 - val const : 'a t -> 'a -> 'a t 1214 + val const : 'a codec -> 'a -> 'a codec 1217 1215 (** [const t v] maps any JSON value to [v] on decodes and unconditionally 1218 1216 encodes [v] with [t]. *) 1219 1217 1220 - val recode : dec:'a t -> ('a -> 'b) -> enc:'b t -> 'b t 1218 + val recode : dec:'a codec -> ('a -> 'b) -> enc:'b codec -> 'b codec 1221 1219 (** [recode ~dec f ~enc] maps on decodes like [dec] does followed by [f] and on 1222 1220 encodes uses [enc]. This can be used to change the JSON sort of value. For 1223 1221 example: ··· 1226 1224 ]} 1227 1225 decodes an integer but encodes the integer as a string. *) 1228 1226 1229 - val update : 'a t -> json t 1227 + val update : 'a codec -> t codec 1230 1228 (** [update t] decodes any JSON with [t] and directly encodes it back with [t] 1231 1229 to yield the decode result. Encodes any JSON like {!val-json} does. *) 1232 1230 1233 1231 (** {2:array_queries Arrays} *) 1234 1232 1235 - val nth : ?absent:'a -> int -> 'a t -> 'a t 1233 + val nth : ?absent:'a -> int -> 'a codec -> 'a codec 1236 1234 (** [nth n t] decodes the [n]th index of a JSON array with [t]. Other indices 1237 1235 are skipped. The decode errors if there is no such index unless [absent] is 1238 1236 specified in which case this value is returned. Encodes a singleton array. 1239 1237 *) 1240 1238 1241 - val set_nth : ?stub:json -> ?allow_absent:bool -> 'a t -> int -> 'a -> json t 1239 + val set_nth : 1240 + ?stub:json -> ?allow_absent:bool -> 'a codec -> int -> 'a -> t codec 1242 1241 (** [set_nth t n v] on decodes sets the [n]th value of a JSON array to [v] 1243 1242 encoded by [t]. Other indices are left untouched. Errors if there is no such 1244 1243 index unless [~allow_absent:true] is specified in which case the index is ··· 1246 1245 {!Json.zero} applied to the value [v] encoded by [t] (i.e. the "natural 1247 1246 zero" of [v]'s encoding sort). Encodes like {!json_array} does. *) 1248 1247 1249 - val update_nth : ?stub:json -> ?absent:'a -> int -> 'a t -> json t 1248 + val update_nth : ?stub:json -> ?absent:'a -> int -> 'a codec -> t codec 1250 1249 (** [update_nth n t] on decode recodes the [n]th value of a JSON array with [t]. 1251 1250 Errors if there is no such index unless [absent] is specified in which case 1252 1251 the index is created with [absent], encoded with [t] and preceeded by as 1253 1252 many [stub] values as needed. [stub] defaults to {!Json.zero} applied to the 1254 1253 recode. Encodes like {!json_array} does. *) 1255 1254 1256 - val delete_nth : ?allow_absent:bool -> int -> json t 1255 + val delete_nth : ?allow_absent:bool -> int -> t codec 1257 1256 (** [delete_nth n] drops the [n]th index of a JSON array on both decode and 1258 1257 encodes. Other indices are left untouched. Errors if there is no such index 1259 1258 unless [~allow_absent:true] is specified in which case the data is left 1260 1259 untouched. *) 1261 1260 1262 - val filter_map_array : 'a t -> 'b t -> (int -> 'a -> 'b option) -> json t 1261 + val filter_map_array : 1262 + 'a codec -> 'b codec -> (int -> 'a -> 'b option) -> t codec 1263 1263 (** [filter_map_array a b f] maps the [a] elements of a JSON array with [f] to 1264 1264 [b] elements or deletes them on [None]. Encodes generic JSON arrays like 1265 1265 {!json_array} does. *) 1266 1266 1267 - val fold_array : 'a t -> (int -> 'a -> 'b -> 'b) -> 'b -> 'b t 1267 + val fold_array : 'a codec -> (int -> 'a -> 'b -> 'b) -> 'b -> 'b codec 1268 1268 (** [fold_array t f acc] fold [f] over the [t] elements of a JSON array starting 1269 1269 with [acc]. Encodes an empty JSON array. *) 1270 1270 1271 1271 (** {2:object_queries Objects} *) 1272 1272 1273 - val mem : ?absent:'a -> string -> 'a t -> 'a t 1273 + val mem : ?absent:'a -> string -> 'a codec -> 'a codec 1274 1274 (** [mem name t] decodes the member named [name] of a JSON object with [t]. 1275 1275 Other members are skipped. The decode errors if there is no such member 1276 1276 unless [absent] is specified in which case this value is returned. Encodes 1277 1277 an object with a single [name] member. *) 1278 1278 1279 - val set_mem : ?allow_absent:bool -> 'a t -> string -> 'a -> json t 1279 + val set_mem : ?allow_absent:bool -> 'a codec -> string -> 'a -> t codec 1280 1280 (** [set_mem t name v] sets the member value of [name] of a [JSON] object to an 1281 1281 encoding of [v] with [t]. This happens both on decodes and encodes. Errors 1282 1282 if there is no such member unless [allow_absent:true] is specified in which 1283 1283 case a member is added to the object. *) 1284 1284 1285 - val update_mem : ?absent:'a -> string -> 'a t -> json t 1285 + val update_mem : ?absent:'a -> string -> 'a codec -> t codec 1286 1286 (** [update_mem name t] recodes the member value of [name] of a JSON object with 1287 1287 [t]. This happens both on decodes and encodes. Errors if there is no such 1288 1288 member unless [absent] is specified in which case a member with this value 1289 1289 encoded with [t] is added to the object. *) 1290 1290 1291 - val delete_mem : ?allow_absent:bool -> string -> json t 1291 + val delete_mem : ?allow_absent:bool -> string -> t codec 1292 1292 (** [delete_mem name] deletes the member named [name] of a JSON object on 1293 1293 decode. Other members are left untouched. The decode errors if there is no 1294 1294 such member unless [~allow_absent:true] is specified in which case the data 1295 1295 is left untouched. Encodes generic JSON objects like {!json_object} does. *) 1296 1296 1297 1297 val filter_map_object : 1298 - 'a t -> 'b t -> (Meta.t -> string -> 'a -> (name * 'b) option) -> json t 1298 + 'a codec -> 'b codec -> (Meta.t -> string -> 'a -> (name * 'b) option) -> t t 1299 1299 (** [filter_map_object a b f] maps the [a] members of a JSON object with [f] to 1300 1300 [(n, b)] members or deletes them on [None]. The meta given to [f] is the 1301 1301 meta of the member name. Encodes generic JSON arrays like {!json_object} 1302 1302 does. *) 1303 1303 1304 - val fold_object : 'a t -> (Meta.t -> string -> 'a -> 'b -> 'b) -> 'b -> 'b t 1304 + val fold_object : 1305 + 'a codec -> (Meta.t -> string -> 'a -> 'b -> 'b) -> 'b -> 'b codec 1305 1306 (** [fold_object t f acc] folds [f] over the [t] members of a JSON object 1306 1307 starting with [acc]. Encodes an empty JSON object. *) 1307 1308 1308 1309 (** {2:index_queries Indices} *) 1309 1310 1310 - val index : ?absent:'a -> Path.index -> 'a t -> 'a t 1311 + val index : ?absent:'a -> Path.index -> 'a codec -> 'a codec 1311 1312 (** [index] uses {!val-nth} or {!val-mem} on the given index. *) 1312 1313 1313 - val set_index : ?allow_absent:bool -> 'a t -> Path.index -> 'a -> json t 1314 + val set_index : ?allow_absent:bool -> 'a codec -> Path.index -> 'a -> t codec 1314 1315 (** [set_index] uses {!set_nth} or {!set_mem} on the given index. *) 1315 1316 1316 - val update_index : ?stub:json -> ?absent:'a -> Path.index -> 'a t -> json t 1317 + val update_index : ?stub:json -> ?absent:'a -> Path.index -> 'a codec -> t codec 1317 1318 (** [update_index] uses {!update_nth} or {!update_mem} on the given index. *) 1318 1319 1319 - val delete_index : ?allow_absent:bool -> Path.index -> json t 1320 + val delete_index : ?allow_absent:bool -> Path.index -> t codec 1320 1321 (** [delete_index] uses {!delete_nth} or {!delete_mem} on the given index. *) 1321 1322 1322 1323 (** {2:path_queries Paths} *) 1323 1324 1324 - val path : ?absent:'a -> Path.t -> 'a t -> 'a t 1325 + val path : ?absent:'a -> Path.t -> 'a codec -> 'a codec 1325 1326 (** [path p t] {{!index}decodes} with [t] on the last index of [p]. If [p] is 1326 1327 {!Path.root} this is [t]. *) 1327 1328 1328 1329 val set_path : 1329 - ?stub:json -> ?allow_absent:bool -> 'a t -> Path.t -> 'a -> json t 1330 + ?stub:json -> ?allow_absent:bool -> 'a codec -> Path.t -> 'a -> t codec 1330 1331 (** [set_path t p v] {{!set_index}sets} the last index of [p]. If [p] is 1331 1332 {!Path.root} this encodes [v] with [t]. *) 1332 1333 1333 - val update_path : ?stub:json -> ?absent:'a -> Path.t -> 'a t -> json t 1334 + val update_path : ?stub:json -> ?absent:'a -> Path.t -> 'a codec -> t codec 1334 1335 (** [update_path p t] {{!update_index}updates} the last index of [p] with [t]. 1335 1336 On the root path this is [t]. *) 1336 1337 1337 - val delete_path : ?allow_absent:bool -> Path.t -> json t 1338 + val delete_path : ?allow_absent:bool -> Path.t -> t codec 1338 1339 (** [delete_path p] {{!delete_index}deletes} the last index of [p]. If [p] is 1339 1340 {!Path.root} this is {!Json.val-null}. *) 1340 1341 ··· 1372 1373 (** [pp_string] formats a JSON string (quoted and escaped). Assumes the string 1373 1374 is valid UTF-8. *) 1374 1375 1375 - val pp_json : json fmt 1376 + val pp_json : t fmt 1376 1377 (** [pp_json] formats JSON, see {!pp_json'}. *) 1377 1378 1378 - val pp_json' : ?number_format:number_format -> unit -> json fmt 1379 + val pp_json' : ?number_format:number_format -> unit -> t fmt 1379 1380 (** [pp' ~format ~number_format () ppf j] formats [j] on [ppf]. The output is 1380 1381 indented but may be more compact than an [Indent] JSON encoder may do. For 1381 1382 example arrays may be output on one line if they fit etc. ··· 1385 1386 ({{!page-cookbook.non_finite_numbers}explanation}). 1386 1387 - Strings are assumed to be valid UTF-8. *) 1387 1388 1388 - val pp_value : ?number_format:number_format -> 'a t -> unit -> 'a fmt 1389 + val pp_value : ?number_format:number_format -> 'a codec -> unit -> 'a fmt 1389 1390 (** [pp_value t ()] formats the JSON representation of values as described by 1390 1391 [t] by encoding it with {!Json.val-encode} and formatting it with 1391 1392 {!pp_json'}. If the encoding of the value errors a JSON string with the ··· 1411 1412 {{:https://erratique.ch/repos/jsont/tree/paper}paper} in the Json source 1412 1413 repository may also help to understand this menagerie of types. *) 1413 1414 module Repr : sig 1414 - type 'a t' := 'a t 1415 + type 'a t' := 'a codec 1415 1416 1416 1417 module String_map : Map.S with type key = string 1417 1418 (** A [Map.Make(String)] instance. *) ··· 1421 1422 type (_, _) eq = Equal : ('a, 'a) eq 1422 1423 1423 1424 module Id : sig 1424 - type 'a t 1425 + type 'a codec 1425 1426 1426 - val make : unit -> 'a t 1427 - val uid : 'a t -> int 1428 - val provably_equal : 'a t -> 'b t -> ('a, 'b) eq option 1427 + val make : unit -> 'a codec 1428 + val uid : 'a codec -> int 1429 + val provably_equal : 'a codec -> 'b codec -> ('a, 'b) eq option 1429 1430 end 1430 1431 end 1431 1432 ··· 1460 1461 (** {1:types JSON types} *) 1461 1462 1462 1463 (** The type for JSON types. *) 1463 - type 'a t = 1464 - | Null : (unit, 'a) base_map -> 'a t (** Null maps. *) 1465 - | Bool : (bool, 'a) base_map -> 'a t (** Boolean maps. *) 1466 - | Number : (float, 'a) base_map -> 'a t (** Number maps. *) 1467 - | String : (string, 'a) base_map -> 'a t (** String maps. *) 1468 - | Array : ('a, 'elt, 'builder) array_map -> 'a t (** Array maps. *) 1464 + type 'a codec = 1465 + | Null : (unit, 'a) base_map -> 'a codec (** Null maps. *) 1466 + | Bool : (bool, 'a) base_map -> 'a codec (** Boolean maps. *) 1467 + | Number : (float, 'a) base_map -> 'a codec (** Number maps. *) 1468 + | String : (string, 'a) base_map -> 'a codec (** String maps. *) 1469 + | Array : ('a, 'elt, 'builder) array_map -> 'a codec (** Array maps. *) 1469 1470 | Object : ('o, 'o) object_map -> 'o t (** Object maps. *) 1470 - | Any : 'a any_map -> 'a t (** Map for different sorts of JSON values. *) 1471 - | Map : ('b, 'a) map -> 'a t 1471 + | Any : 'a any_map -> 'a codec 1472 + (** Map for different sorts of JSON values. *) 1473 + | Map : ('b, 'a) map -> 'a codec 1472 1474 (** Map from JSON type ['b] to JSON type ['a]. *) 1473 - | Rec : 'a t Lazy.t -> 'a t (** Recursive definition. *) 1474 - | Ignore : unit t 1475 + | Rec : 'a codec Lazy.t -> 'a codec (** Recursive definition. *) 1476 + | Ignore : unit codec 1475 1477 (** Skip-parse any JSON value. The bytesrw decoder consumes the value at 1476 1478 the byte level without materialising strings, numbers or nested DOM; 1477 1479 this is the fast path for {!Json.ignore}. *) ··· 1534 1536 and ('o, 'a) mem_map = { 1535 1537 name : string; (** The JSON member name. *) 1536 1538 doc : string; (** Documentation for the JSON member. *) 1537 - type' : 'a t; (** The type for the member value. *) 1539 + type' : 'a codec; (** The type for the member value. *) 1538 1540 id : 'a Type.Id.t; 1539 1541 (** A type identifier for the member. This allows to store the decode in 1540 1542 a {!Dict.t} on decode and give it in time to the object decoding ··· 1582 1584 and ('mems, 'a, 'builder) mems_map = { 1583 1585 kind : string; (** The kind for unknown members (documentation). *) 1584 1586 doc : string; (** Documentation string for the unknown members. *) 1585 - mems_type : 'a t; 1587 + mems_type : 'a codec; 1586 1588 (** The uniform type according which unknown members are typed. *) 1587 1589 id : 'mems Type.Id.t; (** A type identifier for the unknown member map. *) 1588 1590 dec_empty : unit -> 'builder; ··· 1650 1652 and 'a any_map = { 1651 1653 kind : string; (** The kind of JSON values mapped (documentation). *) 1652 1654 doc : string; (** Documentation string for the kind of values. *) 1653 - dec_null : 'a t option; 1655 + dec_null : 'a codec option; 1654 1656 (** [dec_null], if any, is used for decoding JSON nulls. *) 1655 - dec_bool : 'a t option; 1657 + dec_bool : 'a codec option; 1656 1658 (** [dec_bool], if any, is used for decoding JSON bools. *) 1657 - dec_number : 'a t option; 1659 + dec_number : 'a codec option; 1658 1660 (** [dec_number], if any, is used for decoding JSON numbers. *) 1659 - dec_string : 'a t option; 1661 + dec_string : 'a codec option; 1660 1662 (** [dec_string], if any, is used for decoding JSON strings. *) 1661 - dec_array : 'a t option; 1663 + dec_array : 'a codec option; 1662 1664 (** [dec_array], if any, is used for decoding JSON arrays. *) 1663 - dec_object : 'a t option; 1665 + dec_object : 'a codec option; 1664 1666 (** [dec_object], if any, is used for decoding JSON objects. *) 1665 - enc : 'a -> 'a t; 1667 + enc : 'a -> 'a codec; 1666 1668 (** [enc] specifies the encoder to use on a given value. *) 1667 1669 } 1668 1670 (** The type for mapping JSON values with multiple sorts to a value of type ··· 1674 1676 and ('a, 'b) map = { 1675 1677 kind : string; (** The kind of JSON values mapped (documentation). *) 1676 1678 doc : string; (** Documentation string for the kind of values. *) 1677 - dom : 'a t; (** The domain of the map. *) 1679 + dom : 'a codec; (** The domain of the map. *) 1678 1680 dec : 'a -> 'b; (** [dec] decodes ['a] to ['b]. *) 1679 1681 enc : 'b -> 'a; (** [enc] encodes ['b] to ['a]. *) 1680 1682 } ··· 1683 1685 1684 1686 (** {1:conv Convert} *) 1685 1687 1686 - val of_t : 'a t' -> 'a t 1688 + val of_t : 'a t' -> 'a codec 1687 1689 (** [of_t] is {!Stdlib.Fun.id}. *) 1688 1690 1689 - val unsafe_to_t : 'a t -> 'a t' 1691 + val unsafe_to_t : 'a codec -> 'a t' 1690 1692 (** [unsafe_to_t r] converts the representation to a type [r]. It is unsafe 1691 1693 because constructors of the {!Json} module do maintain some invariants. *) 1692 1694 1693 1695 (** {1:kinds Kinds and doc} *) 1694 1696 1695 - val kinded_sort : 'a t -> string 1697 + val kinded_sort : 'a codec -> string 1696 1698 (** [kinded_sort t] is kinded sort of [t], see {!Json.kinded_sort}. *) 1697 1699 1698 1700 val array_map_kinded_sort : ('a, 'elt, 'builder) array_map -> string ··· 1706 1708 val pp_kind : string fmt 1707 1709 (** [pp_kind] formats kinds. *) 1708 1710 1709 - val doc : 'a t -> string 1711 + val doc : 'a codec -> string 1710 1712 (** See {!Json.doc}. *) 1711 1713 1712 - val with_doc : ?kind:string -> ?doc:string -> 'a t -> 'a t 1714 + val with_doc : ?kind:string -> ?doc:string -> 'a codec -> 'a codec 1713 1715 (** See {!Json.with_doc}. *) 1714 1716 1715 1717 (** {1:errors Errors} *) ··· 1724 1726 (** [error_push_object] is like {!Error.push_object} but uses the given object 1725 1727 [meta] and object map to caracterize the context. *) 1726 1728 1727 - val type_error : Meta.t -> 'a t -> fnd:Sort.t -> 'b 1729 + val type_error : Meta.t -> 'a codec -> fnd:Sort.t -> 'b 1728 1730 (** [type_error meta ~exp ~fnd] errors when kind [exp] was expected but sort 1729 1731 [fnd] was found. *) 1730 1732
lib/json_base.ml lib/core.ml
lib/json_base.mli lib/core.mli