···175175176176 (** {1:scalars Scalar codecs} *)
177177178178- module Base : sig
179179- (** Low-level maps for custom scalar codecs. Most users can start with
180180- {!Json.Codec.map}, {!Json.Codec.of_of_string}, or the predefined scalar
181181- codecs. *)
182182-183183- type ('json, 'a) map
184184- (** A bidirectional map between a JSON scalar representation and an OCaml
185185- value. *)
186186-187187- val map :
188188- ?kind:string ->
189189- ?doc:string ->
190190- ?dec:(Meta.t -> 'json -> 'a) ->
191191- ?enc:('a -> 'json) ->
192192- ?enc_meta:('a -> Meta.t) ->
193193- unit ->
194194- ('json, 'a) map
195195- (** [map ()] builds a scalar map. Omit [dec] for encode-only maps and [enc]
196196- for decode-only maps. *)
197197-198198- val id : ('a, 'a) map
199199- (** The identity map. *)
200200-201201- val ignore : ('a, unit) map
202202- (** A map that ignores decoded values and errors on encoding. *)
203203-204204- val null : (unit, 'a) map -> 'a t
205205- (** Build a codec over JSON nulls. *)
206206-207207- val bool : (bool, 'a) map -> 'a t
208208- (** Build a codec over JSON booleans. *)
209209-210210- val number : (float, 'a) map -> 'a t
211211- (** Build a codec over JSON numbers, represented as OCaml [float]s. *)
212212-213213- val string : (string, 'a) map -> 'a t
214214- (** Build a codec over JSON strings, represented as UTF-8 OCaml strings. *)
215215-216216- val dec : ('json -> 'a) -> Meta.t -> 'json -> 'a
217217- (** Adapt a total decoding function. *)
218218-219219- val dec_result :
220220- ?kind:string -> ('json -> ('a, string) result) -> Meta.t -> 'json -> 'a
221221- (** Adapt a result-returning decoding function. *)
222222-223223- val dec_failure : ?kind:string -> ('json -> 'a) -> Meta.t -> 'json -> 'a
224224- (** Adapt a decoding function that may raise [Failure]. *)
225225-226226- val enc : ('a -> 'json) -> 'a -> 'json
227227- (** Adapt a total encoding function. *)
228228-229229- val enc_result :
230230- ?kind:string -> ('a -> ('json, string) result) -> 'a -> 'json
231231- (** Adapt a result-returning encoding function. *)
232232-233233- val enc_failure : ?kind:string -> ('a -> 'json) -> 'a -> 'json
234234- (** Adapt an encoding function that may raise [Failure]. *)
235235- end
236236-237178 val null : ?kind:string -> ?doc:string -> 'a -> 'a t
238179 (** [null v] maps JSON nulls to [v] and encodes all values as JSON null. *)
239180···315256 (** Binary strings encoded as lower-case hexadecimal JSON strings. *)
316257317258 (** {1:arrays Array and tuple codecs} *)
318318-319319- module Array : sig
320320- (** Low-level maps for custom JSON array representations. Prefer
321321- {!Json.Codec.list}, {!Json.Codec.array}, and the tuple helpers when they
322322- fit. *)
323323-324324- type ('array, 'elt) enc = {
325325- enc : 'acc. ('acc -> int -> 'elt -> 'acc) -> 'acc -> 'array -> 'acc;
326326- }
327327- (** A fold over the elements to encode. *)
328328-329329- type ('array, 'elt, 'builder) map
330330- (** A bidirectional map for JSON arrays. *)
331331-332332- val map :
333333- ?kind:string ->
334334- ?doc:string ->
335335- ?dec_empty:(unit -> 'builder) ->
336336- ?dec_skip:(int -> 'builder -> bool) ->
337337- ?dec_add:(int -> 'elt -> 'builder -> 'builder) ->
338338- ?dec_finish:(Meta.t -> int -> 'builder -> 'array) ->
339339- ?enc:('array, 'elt) enc ->
340340- ?enc_meta:('array -> Meta.t) ->
341341- 'elt t ->
342342- ('array, 'elt, 'builder) map
343343- (** Build a custom JSON array map. *)
344344-345345- val list_map :
346346- ?kind:string ->
347347- ?doc:string ->
348348- ?dec_skip:(int -> 'a list -> bool) ->
349349- 'a t ->
350350- ('a list, 'a, 'a list) map
351351- (** A map for OCaml lists. *)
352352-353353- type 'a array_builder
354354- (** Builder state for OCaml arrays. *)
355355-356356- val array_map :
357357- ?kind:string ->
358358- ?doc:string ->
359359- ?dec_skip:(int -> 'a array_builder -> bool) ->
360360- 'a t ->
361361- ('a array, 'a, 'a array_builder) map
362362- (** A map for OCaml arrays. *)
363363-364364- type ('a, 'b, 'c) bigarray_builder
365365- (** Builder state for bigarrays. *)
366366-367367- val bigarray_map :
368368- ?kind:string ->
369369- ?doc:string ->
370370- ?dec_skip:(int -> ('a, 'b, 'c) bigarray_builder -> bool) ->
371371- ('a, 'b) Bigarray.kind ->
372372- 'c Bigarray.layout ->
373373- 'a t ->
374374- (('a, 'b, 'c) Bigarray.Array1.t, 'a, ('a, 'b, 'c) bigarray_builder) map
375375- (** A map for one-dimensional bigarrays. *)
376376-377377- val array : ('a, _, _) map -> 'a t
378378- (** Build a codec from an array map. *)
379379-380380- val ignore : unit t
381381- (** Ignore JSON arrays on decoding and error on encoding. *)
382382-383383- val zero : unit t
384384- (** Ignore JSON arrays on decoding and encode an empty array. *)
385385- end
386259387260 val list : ?kind:string -> ?doc:string -> 'a t -> 'a list t
388261 (** JSON arrays as OCaml lists. *)
+8-7
test/codecs/cookbook.ml
···1010 let enc = function "" -> null | _ -> Json.Codec.string in
1111 Json.Codec.any ~dec_null:null ~dec_string:Json.Codec.string ~enc ()
12121313-(* Base maps *)
1313+(* String codecs *)
14141515module M = struct
1616 type t = unit
···2121end
22222323let m_codec =
2424- let dec = Json.Codec.Base.dec_result M.result_of_string in
2525- let enc = Json.Codec.Base.enc M.to_string in
2626- Json.Codec.Base.string (Json.Codec.Base.map ~kind:"M.t" ~dec ~enc ())
2424+ Json.Codec.of_of_string ~kind:"M.t" M.result_of_string ~enc:M.to_string
27252826let m_codec' =
2929- let dec = Json.Codec.Base.dec_failure M.of_string_or_failure in
3030- let enc = Json.Codec.Base.enc M.to_string in
3131- Json.Codec.Base.string (Json.Codec.Base.map ~kind:"M.t" ~dec ~enc ())
2727+ let parse s =
2828+ match M.of_string_or_failure s with
2929+ | v -> Ok v
3030+ | exception Failure e -> Error e
3131+ in
3232+ Json.Codec.of_of_string ~kind:"M.t" parse ~enc:M.to_string
32333334let m_codec'' =
3435 Json.Codec.of_of_string ~kind:"M.t" M.result_of_string ~enc:M.to_string
+6-10
test/codecs/geojson.ml
···148148149149 let feature_id_codec =
150150 let number =
151151- let dec = Json.Codec.Base.dec (fun n -> `Number n) in
152152- let enc =
153153- Json.Codec.Base.enc (function `Number n -> n | _ -> assert false)
154154- in
155155- Json.Codec.Base.number (Json.Codec.Base.map ~enc ~dec ())
151151+ let dec n = `Number n in
152152+ let enc = function `Number n -> n | _ -> assert false in
153153+ Json.Codec.map ~dec ~enc Json.Codec.number
156154 in
157155 let string =
158158- let dec = Json.Codec.Base.dec (fun n -> `String n) in
159159- let enc =
160160- Json.Codec.Base.enc (function `String n -> n | _ -> assert false)
161161- in
162162- Json.Codec.Base.string (Json.Codec.Base.map ~enc ~dec ())
156156+ let dec n = `String n in
157157+ let enc = function `String n -> n | _ -> assert false in
158158+ Json.Codec.map ~dec ~enc Json.Codec.string
163159 in
164160 let enc = function `Number _ -> number | `String _ -> string in
165161 Json.Codec.any ~kind:"id" ~dec_number:number ~dec_string:string ~enc ()
+6-10
test/codecs/topojson.ml
···126126127127 let id_codec =
128128 let number =
129129- let dec = Json.Codec.Base.dec (fun n -> `Number n) in
130130- let enc =
131131- Json.Codec.Base.enc (function `Number n -> n | _ -> assert false)
132132- in
133133- Json.Codec.Base.number (Json.Codec.Base.map ~enc ~dec ())
129129+ let dec n = `Number n in
130130+ let enc = function `Number n -> n | _ -> assert false in
131131+ Json.Codec.map ~dec ~enc Json.Codec.number
134132 in
135133 let string =
136136- let dec = Json.Codec.Base.dec (fun n -> `String n) in
137137- let enc =
138138- Json.Codec.Base.enc (function `String n -> n | _ -> assert false)
139139- in
140140- Json.Codec.Base.string (Json.Codec.Base.map ~enc ~dec ())
134134+ let dec n = `String n in
135135+ let enc = function `String n -> n | _ -> assert false in
136136+ Json.Codec.map ~dec ~enc Json.Codec.string
141137 in
142138 let enc = function `Number _ -> number | `String _ -> string in
143139 Json.Codec.any ~kind:"id" ~dec_number:number ~dec_string:string ~enc ()