OCaml Zarr jsont codecs for v2/v3 and common conventions
0
fork

Configure Feed

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

feat: Other_codec and Other_ext escape hatch types

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

+89 -1
+34
src/zarr_jsont.ml
··· 1 1 (* Zarr jsont codecs *) 2 + 3 + module Other_codec = struct 4 + type t = { name : string; configuration : Jsont.json } 5 + 6 + let name c = c.name 7 + let configuration c = c.configuration 8 + let make name configuration = { name; configuration } 9 + 10 + let jsont = 11 + Jsont.Object.map ~kind:"Other_codec" make 12 + |> Jsont.Object.mem "id" Jsont.string ~enc:name 13 + |> Jsont.Object.keep_unknown Jsont.json_mems ~enc:configuration 14 + |> Jsont.Object.finish 15 + end 16 + 17 + module Other_ext = struct 18 + type t = { name : string; configuration : Jsont.json option; must_understand : bool } 19 + 20 + let name e = e.name 21 + let configuration e = e.configuration 22 + let must_understand e = e.must_understand 23 + let make name configuration must_understand = { name; configuration; must_understand } 24 + 25 + let jsont = 26 + Jsont.Object.map ~kind:"Other_ext" make 27 + |> Jsont.Object.mem "name" Jsont.string ~enc:name 28 + |> Jsont.Object.opt_mem "configuration" Jsont.json ~enc:configuration 29 + |> Jsont.Object.mem "must_understand" Jsont.bool 30 + ~dec_absent:true 31 + ~enc_omit:(fun v -> v) 32 + ~enc:must_understand 33 + |> Jsont.Object.skip_unknown 34 + |> Jsont.Object.finish 35 + end
+27
src/zarr_jsont.mli
··· 1 1 (** Jsont codecs for Zarr v2 and v3 metadata. *) 2 + 3 + (** Catch-all type for unrecognized v2 codecs. 4 + 5 + Represents objects with an ["id"] key plus arbitrary extra fields, 6 + for example [{"id":"custom_codec","param1":42}]. Unknown fields are 7 + preserved and round-tripped faithfully. *) 8 + module Other_codec : sig 9 + type t 10 + val name : t -> string 11 + val configuration : t -> Jsont.json 12 + val make : string -> Jsont.json -> t 13 + val jsont : t Jsont.t 14 + end 15 + 16 + (** Catch-all type for unrecognized v3 extensions. 17 + 18 + Represents objects with a ["name"] key, an optional ["configuration"] 19 + object, and a ["must_understand"] boolean (defaults to [true] when 20 + absent; omitted on encode when [true]). Unknown fields are skipped. *) 21 + module Other_ext : sig 22 + type t 23 + val name : t -> string 24 + val configuration : t -> Jsont.json option 25 + val must_understand : t -> bool 26 + val make : string -> Jsont.json option -> bool -> t 27 + val jsont : t Jsont.t 28 + end
+28 -1
test/test_zarr_jsont.ml
··· 1 - let () = print_endline "zarr-jsont tests: ok" 1 + let decode c s = match Jsont_bytesrw.decode_string c s with 2 + | Ok v -> v | Error e -> failwith e 3 + 4 + let encode c v = match Jsont_bytesrw.encode_string c v with 5 + | Ok s -> s | Error e -> failwith e 6 + 7 + let test_other_codec () = 8 + let json = {|{"id":"custom_codec","param1":42,"param2":"hello"}|} in 9 + let v = decode Zarr_jsont.Other_codec.jsont json in 10 + assert (Zarr_jsont.Other_codec.name v = "custom_codec"); 11 + let json' = encode Zarr_jsont.Other_codec.jsont v in 12 + let v' = decode Zarr_jsont.Other_codec.jsont json' in 13 + assert (Zarr_jsont.Other_codec.name v' = "custom_codec"); 14 + print_endline "test_other_codec: ok" 15 + 16 + let test_other_ext () = 17 + let json = {|{"name":"custom.ext","configuration":{"key":"val"},"must_understand":false}|} in 18 + let v = decode Zarr_jsont.Other_ext.jsont json in 19 + assert (Zarr_jsont.Other_ext.name v = "custom.ext"); 20 + assert (Zarr_jsont.Other_ext.must_understand v = false); 21 + assert (Zarr_jsont.Other_ext.configuration v <> None); 22 + let json' = encode Zarr_jsont.Other_ext.jsont v in 23 + let v' = decode Zarr_jsont.Other_ext.jsont json' in 24 + assert (Zarr_jsont.Other_ext.name v' = "custom.ext"); 25 + print_endline "test_other_ext: ok" 26 + 27 + let () = test_other_codec () 28 + let () = test_other_ext ()