···11(* Zarr jsont codecs *)
22+33+module Other_codec = struct
44+ type t = { name : string; configuration : Jsont.json }
55+66+ let name c = c.name
77+ let configuration c = c.configuration
88+ let make name configuration = { name; configuration }
99+1010+ let jsont =
1111+ Jsont.Object.map ~kind:"Other_codec" make
1212+ |> Jsont.Object.mem "id" Jsont.string ~enc:name
1313+ |> Jsont.Object.keep_unknown Jsont.json_mems ~enc:configuration
1414+ |> Jsont.Object.finish
1515+end
1616+1717+module Other_ext = struct
1818+ type t = { name : string; configuration : Jsont.json option; must_understand : bool }
1919+2020+ let name e = e.name
2121+ let configuration e = e.configuration
2222+ let must_understand e = e.must_understand
2323+ let make name configuration must_understand = { name; configuration; must_understand }
2424+2525+ let jsont =
2626+ Jsont.Object.map ~kind:"Other_ext" make
2727+ |> Jsont.Object.mem "name" Jsont.string ~enc:name
2828+ |> Jsont.Object.opt_mem "configuration" Jsont.json ~enc:configuration
2929+ |> Jsont.Object.mem "must_understand" Jsont.bool
3030+ ~dec_absent:true
3131+ ~enc_omit:(fun v -> v)
3232+ ~enc:must_understand
3333+ |> Jsont.Object.skip_unknown
3434+ |> Jsont.Object.finish
3535+end
+27
src/zarr_jsont.mli
···11(** Jsont codecs for Zarr v2 and v3 metadata. *)
22+33+(** Catch-all type for unrecognized v2 codecs.
44+55+ Represents objects with an ["id"] key plus arbitrary extra fields,
66+ for example [{"id":"custom_codec","param1":42}]. Unknown fields are
77+ preserved and round-tripped faithfully. *)
88+module Other_codec : sig
99+ type t
1010+ val name : t -> string
1111+ val configuration : t -> Jsont.json
1212+ val make : string -> Jsont.json -> t
1313+ val jsont : t Jsont.t
1414+end
1515+1616+(** Catch-all type for unrecognized v3 extensions.
1717+1818+ Represents objects with a ["name"] key, an optional ["configuration"]
1919+ object, and a ["must_understand"] boolean (defaults to [true] when
2020+ absent; omitted on encode when [true]). Unknown fields are skipped. *)
2121+module Other_ext : sig
2222+ type t
2323+ val name : t -> string
2424+ val configuration : t -> Jsont.json option
2525+ val must_understand : t -> bool
2626+ val make : string -> Jsont.json option -> bool -> t
2727+ val jsont : t Jsont.t
2828+end
+28-1
test/test_zarr_jsont.ml
···11-let () = print_endline "zarr-jsont tests: ok"
11+let decode c s = match Jsont_bytesrw.decode_string c s with
22+ | Ok v -> v | Error e -> failwith e
33+44+let encode c v = match Jsont_bytesrw.encode_string c v with
55+ | Ok s -> s | Error e -> failwith e
66+77+let test_other_codec () =
88+ let json = {|{"id":"custom_codec","param1":42,"param2":"hello"}|} in
99+ let v = decode Zarr_jsont.Other_codec.jsont json in
1010+ assert (Zarr_jsont.Other_codec.name v = "custom_codec");
1111+ let json' = encode Zarr_jsont.Other_codec.jsont v in
1212+ let v' = decode Zarr_jsont.Other_codec.jsont json' in
1313+ assert (Zarr_jsont.Other_codec.name v' = "custom_codec");
1414+ print_endline "test_other_codec: ok"
1515+1616+let test_other_ext () =
1717+ let json = {|{"name":"custom.ext","configuration":{"key":"val"},"must_understand":false}|} in
1818+ let v = decode Zarr_jsont.Other_ext.jsont json in
1919+ assert (Zarr_jsont.Other_ext.name v = "custom.ext");
2020+ assert (Zarr_jsont.Other_ext.must_understand v = false);
2121+ assert (Zarr_jsont.Other_ext.configuration v <> None);
2222+ let json' = encode Zarr_jsont.Other_ext.jsont v in
2323+ let v' = decode Zarr_jsont.Other_ext.jsont json' in
2424+ assert (Zarr_jsont.Other_ext.name v' = "custom.ext");
2525+ print_endline "test_other_ext: ok"
2626+2727+let () = test_other_codec ()
2828+let () = test_other_ext ()