Declarative JSON data manipulation for OCaml
0
fork

Configure Feed

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

Clean JSON public API docs

+27 -154
+4
dune
··· 1 1 (env 2 2 (dev 3 3 (flags :standard %{dune-warnings}))) 4 + 5 + (mdx 6 + (files README.md) 7 + (libraries json))
+2
dune-project
··· 1 1 (lang dune 3.21) 2 + (using mdx 0.4) 2 3 3 4 (name json) 4 5 ··· 24 25 (dune (>= 3.21)) 25 26 (bytesrw (>= 0.1.0)) 26 27 (alcotest :with-test) 28 + (mdx :with-test) 27 29 (odoc :with-doc) 28 30 brr 29 31 fmt
+1
json.opam
··· 18 18 "dune" {>= "3.21" & >= "3.21"} 19 19 "bytesrw" {>= "0.1.0"} 20 20 "alcotest" {with-test} 21 + "mdx" {with-test} 21 22 "odoc" {with-doc} 22 23 "brr" 23 24 "fmt"
-127
lib/json.mli
··· 175 175 176 176 (** {1:scalars Scalar codecs} *) 177 177 178 - module Base : sig 179 - (** Low-level maps for custom scalar codecs. Most users can start with 180 - {!Json.Codec.map}, {!Json.Codec.of_of_string}, or the predefined scalar 181 - codecs. *) 182 - 183 - type ('json, 'a) map 184 - (** A bidirectional map between a JSON scalar representation and an OCaml 185 - value. *) 186 - 187 - val map : 188 - ?kind:string -> 189 - ?doc:string -> 190 - ?dec:(Meta.t -> 'json -> 'a) -> 191 - ?enc:('a -> 'json) -> 192 - ?enc_meta:('a -> Meta.t) -> 193 - unit -> 194 - ('json, 'a) map 195 - (** [map ()] builds a scalar map. Omit [dec] for encode-only maps and [enc] 196 - for decode-only maps. *) 197 - 198 - val id : ('a, 'a) map 199 - (** The identity map. *) 200 - 201 - val ignore : ('a, unit) map 202 - (** A map that ignores decoded values and errors on encoding. *) 203 - 204 - val null : (unit, 'a) map -> 'a t 205 - (** Build a codec over JSON nulls. *) 206 - 207 - val bool : (bool, 'a) map -> 'a t 208 - (** Build a codec over JSON booleans. *) 209 - 210 - val number : (float, 'a) map -> 'a t 211 - (** Build a codec over JSON numbers, represented as OCaml [float]s. *) 212 - 213 - val string : (string, 'a) map -> 'a t 214 - (** Build a codec over JSON strings, represented as UTF-8 OCaml strings. *) 215 - 216 - val dec : ('json -> 'a) -> Meta.t -> 'json -> 'a 217 - (** Adapt a total decoding function. *) 218 - 219 - val dec_result : 220 - ?kind:string -> ('json -> ('a, string) result) -> Meta.t -> 'json -> 'a 221 - (** Adapt a result-returning decoding function. *) 222 - 223 - val dec_failure : ?kind:string -> ('json -> 'a) -> Meta.t -> 'json -> 'a 224 - (** Adapt a decoding function that may raise [Failure]. *) 225 - 226 - val enc : ('a -> 'json) -> 'a -> 'json 227 - (** Adapt a total encoding function. *) 228 - 229 - val enc_result : 230 - ?kind:string -> ('a -> ('json, string) result) -> 'a -> 'json 231 - (** Adapt a result-returning encoding function. *) 232 - 233 - val enc_failure : ?kind:string -> ('a -> 'json) -> 'a -> 'json 234 - (** Adapt an encoding function that may raise [Failure]. *) 235 - end 236 - 237 178 val null : ?kind:string -> ?doc:string -> 'a -> 'a t 238 179 (** [null v] maps JSON nulls to [v] and encodes all values as JSON null. *) 239 180 ··· 315 256 (** Binary strings encoded as lower-case hexadecimal JSON strings. *) 316 257 317 258 (** {1:arrays Array and tuple codecs} *) 318 - 319 - module Array : sig 320 - (** Low-level maps for custom JSON array representations. Prefer 321 - {!Json.Codec.list}, {!Json.Codec.array}, and the tuple helpers when they 322 - fit. *) 323 - 324 - type ('array, 'elt) enc = { 325 - enc : 'acc. ('acc -> int -> 'elt -> 'acc) -> 'acc -> 'array -> 'acc; 326 - } 327 - (** A fold over the elements to encode. *) 328 - 329 - type ('array, 'elt, 'builder) map 330 - (** A bidirectional map for JSON arrays. *) 331 - 332 - val map : 333 - ?kind:string -> 334 - ?doc:string -> 335 - ?dec_empty:(unit -> 'builder) -> 336 - ?dec_skip:(int -> 'builder -> bool) -> 337 - ?dec_add:(int -> 'elt -> 'builder -> 'builder) -> 338 - ?dec_finish:(Meta.t -> int -> 'builder -> 'array) -> 339 - ?enc:('array, 'elt) enc -> 340 - ?enc_meta:('array -> Meta.t) -> 341 - 'elt t -> 342 - ('array, 'elt, 'builder) map 343 - (** Build a custom JSON array map. *) 344 - 345 - val list_map : 346 - ?kind:string -> 347 - ?doc:string -> 348 - ?dec_skip:(int -> 'a list -> bool) -> 349 - 'a t -> 350 - ('a list, 'a, 'a list) map 351 - (** A map for OCaml lists. *) 352 - 353 - type 'a array_builder 354 - (** Builder state for OCaml arrays. *) 355 - 356 - val array_map : 357 - ?kind:string -> 358 - ?doc:string -> 359 - ?dec_skip:(int -> 'a array_builder -> bool) -> 360 - 'a t -> 361 - ('a array, 'a, 'a array_builder) map 362 - (** A map for OCaml arrays. *) 363 - 364 - type ('a, 'b, 'c) bigarray_builder 365 - (** Builder state for bigarrays. *) 366 - 367 - val bigarray_map : 368 - ?kind:string -> 369 - ?doc:string -> 370 - ?dec_skip:(int -> ('a, 'b, 'c) bigarray_builder -> bool) -> 371 - ('a, 'b) Bigarray.kind -> 372 - 'c Bigarray.layout -> 373 - 'a t -> 374 - (('a, 'b, 'c) Bigarray.Array1.t, 'a, ('a, 'b, 'c) bigarray_builder) map 375 - (** A map for one-dimensional bigarrays. *) 376 - 377 - val array : ('a, _, _) map -> 'a t 378 - (** Build a codec from an array map. *) 379 - 380 - val ignore : unit t 381 - (** Ignore JSON arrays on decoding and error on encoding. *) 382 - 383 - val zero : unit t 384 - (** Ignore JSON arrays on decoding and encode an empty array. *) 385 - end 386 259 387 260 val list : ?kind:string -> ?doc:string -> 'a t -> 'a list t 388 261 (** JSON arrays as OCaml lists. *)
+8 -7
test/codecs/cookbook.ml
··· 10 10 let enc = function "" -> null | _ -> Json.Codec.string in 11 11 Json.Codec.any ~dec_null:null ~dec_string:Json.Codec.string ~enc () 12 12 13 - (* Base maps *) 13 + (* String codecs *) 14 14 15 15 module M = struct 16 16 type t = unit ··· 21 21 end 22 22 23 23 let m_codec = 24 - let dec = Json.Codec.Base.dec_result M.result_of_string in 25 - let enc = Json.Codec.Base.enc M.to_string in 26 - Json.Codec.Base.string (Json.Codec.Base.map ~kind:"M.t" ~dec ~enc ()) 24 + Json.Codec.of_of_string ~kind:"M.t" M.result_of_string ~enc:M.to_string 27 25 28 26 let m_codec' = 29 - let dec = Json.Codec.Base.dec_failure M.of_string_or_failure in 30 - let enc = Json.Codec.Base.enc M.to_string in 31 - Json.Codec.Base.string (Json.Codec.Base.map ~kind:"M.t" ~dec ~enc ()) 27 + let parse s = 28 + match M.of_string_or_failure s with 29 + | v -> Ok v 30 + | exception Failure e -> Error e 31 + in 32 + Json.Codec.of_of_string ~kind:"M.t" parse ~enc:M.to_string 32 33 33 34 let m_codec'' = 34 35 Json.Codec.of_of_string ~kind:"M.t" M.result_of_string ~enc:M.to_string
+6 -10
test/codecs/geojson.ml
··· 148 148 149 149 let feature_id_codec = 150 150 let number = 151 - let dec = Json.Codec.Base.dec (fun n -> `Number n) in 152 - let enc = 153 - Json.Codec.Base.enc (function `Number n -> n | _ -> assert false) 154 - in 155 - Json.Codec.Base.number (Json.Codec.Base.map ~enc ~dec ()) 151 + let dec n = `Number n in 152 + let enc = function `Number n -> n | _ -> assert false in 153 + Json.Codec.map ~dec ~enc Json.Codec.number 156 154 in 157 155 let string = 158 - let dec = Json.Codec.Base.dec (fun n -> `String n) in 159 - let enc = 160 - Json.Codec.Base.enc (function `String n -> n | _ -> assert false) 161 - in 162 - Json.Codec.Base.string (Json.Codec.Base.map ~enc ~dec ()) 156 + let dec n = `String n in 157 + let enc = function `String n -> n | _ -> assert false in 158 + Json.Codec.map ~dec ~enc Json.Codec.string 163 159 in 164 160 let enc = function `Number _ -> number | `String _ -> string in 165 161 Json.Codec.any ~kind:"id" ~dec_number:number ~dec_string:string ~enc ()
+6 -10
test/codecs/topojson.ml
··· 126 126 127 127 let id_codec = 128 128 let number = 129 - let dec = Json.Codec.Base.dec (fun n -> `Number n) in 130 - let enc = 131 - Json.Codec.Base.enc (function `Number n -> n | _ -> assert false) 132 - in 133 - Json.Codec.Base.number (Json.Codec.Base.map ~enc ~dec ()) 129 + let dec n = `Number n in 130 + let enc = function `Number n -> n | _ -> assert false in 131 + Json.Codec.map ~dec ~enc Json.Codec.number 134 132 in 135 133 let string = 136 - let dec = Json.Codec.Base.dec (fun n -> `String n) in 137 - let enc = 138 - Json.Codec.Base.enc (function `String n -> n | _ -> assert false) 139 - in 140 - Json.Codec.Base.string (Json.Codec.Base.map ~enc ~dec ()) 134 + let dec n = `String n in 135 + let enc = function `String n -> n | _ -> assert false in 136 + Json.Codec.map ~dec ~enc Json.Codec.string 141 137 in 142 138 let enc = function `Number _ -> number | `String _ -> string in 143 139 Json.Codec.any ~kind:"id" ~dec_number:number ~dec_string:string ~enc ()