Declarative JSON data manipulation for OCaml
0
fork

Configure Feed

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

ocaml-json: use Fmt instead of Format, drop 'a fmt alias

Add fmt as a library dep and migrate to it:

- Core.Fmt's trivial Format wrappers (pf, str, nop, sp, char, string,
list, lines) delegate to Fmt instead. The list label renames from
?pp_sep to ?sep to match external Fmt.
- Value's pp_json' uses Fmt.char / Fmt.string / Fmt.list where Fmt
provides equivalents. Box/break functions (pp_open_hovbox,
pp_print_break, pp_close_box) stay on Format since Fmt has no
equivalent — matches upstream jsont.
- Drop the redundant [type 'a fmt = Format.formatter -> 'a -> unit]
alias from every file; callers use [Fmt.t] directly.

+51 -62
+13 -21
lib/core.ml
··· 169 169 (* Mini fmt *) 170 170 171 171 module Fmt = struct 172 - type 'a t = Format.formatter -> 'a -> unit 172 + type 'a t = 'a Fmt.t 173 173 174 - let pf = Format.fprintf 175 - let str = Format.asprintf 176 - let nop _ () = () 177 - let sp = Format.pp_print_space 178 - 179 - let comma ppf () = 180 - Format.pp_print_char ppf ','; 181 - sp ppf () 182 - 183 - let list = Format.pp_print_list 184 - let char = Format.pp_print_char 185 - let string = Format.pp_print_string 174 + let pf = Fmt.pf 175 + let str = Fmt.str 176 + let nop = Fmt.nop 177 + let sp = Fmt.sp 178 + let char = Fmt.char 179 + let string = Fmt.string 180 + let list = Fmt.list 181 + let comma ppf () = Fmt.pf ppf ",@ " 186 182 187 183 let substring first len ppf s = 188 - if first = 0 && len = String.length s then string ppf s 184 + if first = 0 && len = String.length s then Fmt.string ppf s 189 185 else 190 - (* One day use https://github.com/ocaml/ocaml/pull/12133 *) 191 186 for i = first to first + len - 1 do 192 - char ppf s.[i] 187 + Fmt.char ppf s.[i] 193 188 done 194 189 195 - let lines ppf s = 196 - Format.pp_print_list string ppf (String.split_on_char '\n' s) 190 + let lines ppf s = Fmt.(list ~sep:cut string) ppf (String.split_on_char '\n' s) 197 191 198 192 (* ANSI styling 199 193 ··· 285 279 match suggest fnd exp with 286 280 | [] -> () 287 281 | ms -> 288 - pf ppf "@;@[Similar members in object: %a@]" (list ~pp_sep:comma code) 289 - ms 282 + pf ppf "@;@[Similar members in object: %a@]" (list ~sep:comma code) ms 290 283 291 284 let should_it_be_mem ppf (exp, fnd) = 292 285 match suggest fnd exp with ··· 353 346 char ppf '"' 354 347 end 355 348 356 - type 'a fmt = Stdlib.Format.formatter -> 'a -> unit 357 349 358 350 (* JSON numbers *) 359 351
+3 -4
lib/core.mli
··· 33 33 val to_bigarray : ('a, 'b, 'c) t -> ('a, 'b, 'c) Bigarray.Array1.t 34 34 end 35 35 36 - (** Mini fmt *) 36 + (** JSON-specific printers and HCI helpers built on top of {!Fmt}. *) 37 37 module Fmt : sig 38 - type 'a t = Format.formatter -> 'a -> unit 38 + type 'a t = 'a Fmt.t 39 39 40 40 val pf : Format.formatter -> ('a, Format.formatter, unit) format -> 'a 41 41 val str : ('a, Format.formatter, unit, string) format4 -> 'a 42 42 val disable_ansi_styler : unit -> unit 43 43 val nop : unit t 44 44 val sp : unit t 45 - val list : ?pp_sep:unit t -> 'a t -> 'a list t 45 + val list : ?sep:unit t -> 'a t -> 'a list t 46 46 val char : char t 47 47 val string : string t 48 48 val substring : int -> int -> string t ··· 65 65 val json_string : string t 66 66 end 67 67 68 - type 'a fmt = Stdlib.Format.formatter -> 'a -> unit 69 68 70 69 (** JSON number tools. *) 71 70 module Number : sig
+1
lib/dune
··· 2 2 (name json) 3 3 (public_name json) 4 4 (libraries 5 + fmt 5 6 (re_export loc)))
-1
lib/json.ml
··· 5 5 6 6 module Fmt = Core.Fmt 7 7 8 - type 'a fmt = 'a Fmt.t 9 8 10 9 let pp_kind = Fmt.code 11 10 let pp_kind_opt ppf kind = if kind = "" then () else pp_kind ppf kind
+16 -17
lib/json.mli
··· 20 20 21 21 (** {1:preliminaries Preliminaries} *) 22 22 23 - type 'a fmt = Format.formatter -> 'a -> unit 24 23 (** The type for formatters of values of type ['a]. *) 25 24 26 25 module Textloc = Loc ··· 123 122 (** [fail meta s] raises an error with empty context and message [s]. *) 124 123 125 124 val failf : Meta.t -> ('a, Stdlib.Format.formatter, unit, 'b) format4 -> 'a 126 - (** [failf meta fmt] is like {!fail} but formats the message. *) 125 + (** [failf meta Fmt.t] is like {!fail} but formats the message. *) 127 126 128 127 val expected : Meta.t -> string -> fnd:string -> 'a 129 - (** [expected meta fmt exp ~fnd] is [msgf "Expected %s but found %s" exp fnd]. 128 + (** [expected meta Fmt.t exp ~fnd] is [msgf "Expected %s but found %s" exp fnd]. 130 129 *) 131 130 132 131 val push_array : string node -> int node -> t -> 'a ··· 151 150 val to_string : t -> string 152 151 (** [error_to_string e] formats [e] using {!val-pp} to a string. *) 153 152 154 - val pp : t fmt 153 + val pp : t Fmt.t 155 154 (** [pp_error] formats errors. *) 156 155 157 - val puterr : unit fmt 156 + val puterr : unit Fmt.t 158 157 (** [puterr] formats [Error:] in red. *) 159 158 160 159 (**/**) ··· 1058 1057 - Objects members are sorted before being compared. 1059 1058 - {!Meta.t} values are ignored. *) 1060 1059 1061 - val pp : t fmt 1060 + val pp : t Fmt.t 1062 1061 (** See {!Json.pp_json}. *) 1063 1062 1064 1063 (** {2:null Nulls and options} *) ··· 1358 1357 finite floating point values can be interchanged without loss of precision. 1359 1358 *) 1360 1359 1361 - val pp_null : unit fmt 1360 + val pp_null : unit Fmt.t 1362 1361 (** [pp_null] formats a JSON null. *) 1363 1362 1364 - val pp_bool : bool fmt 1363 + val pp_bool : bool Fmt.t 1365 1364 (** [pp_bool] formats a JSON bool. *) 1366 1365 1367 - val pp_number : float fmt 1366 + val pp_number : float Fmt.t 1368 1367 (** [pp_number] formats a JSON number of a JSON null if the float is not finite. 1369 1368 Uses the {!default_number_format}. *) 1370 1369 1371 - val pp_number' : number_format -> float fmt 1372 - (** [pp_number fmt] is like {!pp_number} but uses [fmt] to format the number. *) 1370 + val pp_number' : number_format -> float Fmt.t 1371 + (** [pp_number Fmt.t] is like {!pp_number} but uses [fmt] to format the number. *) 1373 1372 1374 - val pp_string : string fmt 1373 + val pp_string : string Fmt.t 1375 1374 (** [pp_string] formats a JSON string (quoted and escaped). Assumes the string 1376 1375 is valid UTF-8. *) 1377 1376 1378 - val pp_json : t fmt 1377 + val pp_json : t Fmt.t 1379 1378 (** [pp_json] formats JSON, see {!pp_json'}. *) 1380 1379 1381 - val pp_json' : ?number_format:number_format -> unit -> t fmt 1380 + val pp_json' : ?number_format:number_format -> unit -> t Fmt.t 1382 1381 (** [pp' ~format ~number_format () ppf j] formats [j] on [ppf]. The output is 1383 1382 indented but may be more compact than an [Indent] JSON encoder may do. For 1384 1383 example arrays may be output on one line if they fit etc. ··· 1388 1387 ({{!page-cookbook.non_finite_numbers}explanation}). 1389 1388 - Strings are assumed to be valid UTF-8. *) 1390 1389 1391 - val pp_value : ?number_format:number_format -> 'a codec -> unit -> 'a fmt 1390 + val pp_value : ?number_format:number_format -> 'a codec -> unit -> 'a Fmt.t 1392 1391 (** [pp_value t ()] formats the JSON representation of values as described by 1393 1392 [t] by encoding it with {!Json.val-encode} and formatting it with 1394 1393 {!pp_json'}. If the encoding of the value errors a JSON string with the ··· 1693 1692 (** [object_map_kind map] is like {!kinded_sort} but acts directly on the 1694 1693 object [map]. *) 1695 1694 1696 - val pp_kind : string fmt 1695 + val pp_kind : string Fmt.t 1697 1696 (** [pp_kind] formats kinds. *) 1698 1697 1699 1698 val doc : 'a t -> string ··· 1788 1787 adds [meta] to [dict] under {!object_meta_arg} and tries to find andd 1789 1788 default values to [dict] for [rem_mems] (and errors if it can't). *) 1790 1789 1791 - val pp_code : string fmt 1790 + val pp_code : string Fmt.t 1792 1791 (** [pp_code] formats strings like code (in bold). *) 1793 1792 end
+10 -10
lib/value.ml
··· 2 2 3 3 module Meta = Loc.Meta 4 4 5 - type 'a fmt = Format.formatter -> 'a -> unit 6 5 type 'a node = 'a * Meta.t 7 6 type name = string node 8 7 ··· 34 33 let pp_json' ?(number_format = default_number_format) () ppf j = 35 34 let pp_indent = 2 in 36 35 let pp_sep ppf () = 37 - Format.pp_print_char ppf ','; 36 + Fmt.char ppf ','; 38 37 Format.pp_print_break ppf 1 pp_indent 39 38 in 40 39 let rec pp_array ppf a = 41 40 Format.pp_open_hovbox ppf 0; 42 - Format.pp_print_char ppf '['; 41 + Fmt.char ppf '['; 43 42 Format.pp_print_break ppf 0 pp_indent; 44 - (Format.pp_print_list ~pp_sep pp_value) ppf a; 43 + (Fmt.list ~sep:pp_sep pp_value) ppf a; 45 44 Format.pp_print_break ppf 0 0; 46 - Format.pp_print_char ppf ']'; 45 + Fmt.char ppf ']'; 47 46 Format.pp_close_box ppf () 48 47 and pp_mem ppf ((m, _), v) = 49 48 Format.pp_open_hvbox ppf 0; 50 49 pp_string ppf m; 51 - Format.pp_print_string ppf ": "; 50 + Fmt.string ppf ": "; 52 51 pp_value ppf v; 53 52 Format.pp_close_box ppf () 54 53 and pp_obj ppf o = 55 54 Format.pp_open_hvbox ppf 0; 56 - Format.pp_print_char ppf '{'; 55 + Fmt.char ppf '{'; 57 56 Format.pp_print_break ppf 0 pp_indent; 58 - (Format.pp_print_list ~pp_sep pp_mem) ppf o; 57 + (Fmt.list ~sep:pp_sep pp_mem) ppf o; 59 58 Format.pp_print_break ppf 0 0; 60 - Format.pp_print_char ppf '}'; 59 + Fmt.char ppf '}'; 61 60 Format.pp_close_box ppf () 62 61 and pp_value ppf = function 63 62 | Null _ -> pp_null ppf () ··· 139 138 let number ?(meta = Meta.none) n = Number (n, meta) 140 139 141 140 let any_float ?(meta = Meta.none) v = 142 - if Float.is_finite v then Number (v, meta) else String (Float.to_string v, meta) 141 + if Float.is_finite v then Number (v, meta) 142 + else String (Float.to_string v, meta) 143 143 144 144 let int32 ?(meta = Meta.none) v = Number (Int32.to_float v, meta) 145 145 let int64_as_string ?(meta = Meta.none) v = String (Int64.to_string v, meta)
+8 -9
lib/value.mli
··· 9 9 10 10 module Meta = Loc.Meta 11 11 12 - type 'a fmt = Format.formatter -> 'a -> unit 13 12 type 'a node = 'a * Meta.t 14 13 type name = string node 15 14 ··· 73 72 74 73 (** {1:printing Pretty-printing} *) 75 74 76 - val pp_null : unit fmt 77 - val pp_bool : bool fmt 78 - val pp_string : string fmt 79 - val pp_number : float fmt 75 + val pp_null : unit Fmt.t 76 + val pp_bool : bool Fmt.t 77 + val pp_string : string Fmt.t 78 + val pp_number : float Fmt.t 80 79 81 80 type number_format = Core.Fmt.json_number_format 82 81 83 82 val default_number_format : number_format 84 - val pp_number' : number_format -> float fmt 85 - val pp_json : t fmt 86 - val pp_json' : ?number_format:number_format -> unit -> t fmt 87 - val pp : t fmt 83 + val pp_number' : number_format -> float Fmt.t 84 + val pp_json : t Fmt.t 85 + val pp_json' : ?number_format:number_format -> unit -> t Fmt.t 86 + val pp : t Fmt.t