···13091309 in
13101310 Jsont.Json.object' mems)
13111311 Jsont.json
13121312+13131313+type t = [ `V2 of V2_node.t | `V3 of V3_node.t ]
13141314+13151315+let jsont : t Jsont.t =
13161316+ let find_zarr_format mems =
13171317+ List.find_map (fun ((n, _), v) ->
13181318+ if n = "zarr_format" then
13191319+ match v with
13201320+ | Jsont.Number (f, _) -> Some (int_of_float f)
13211321+ | _ -> None
13221322+ else None) mems
13231323+ in
13241324+ let has_shape mems =
13251325+ List.exists (fun ((k, _), _) -> k = "shape") mems
13261326+ in
13271327+ Jsont.map ~kind:"Zarr"
13281328+ ~dec:(fun json ->
13291329+ let mems = match json with
13301330+ | Jsont.Object (m, _) -> m
13311331+ | _ -> failwith "jsont: expected object"
13321332+ in
13331333+ let zarr_format = match find_zarr_format mems with
13341334+ | Some n -> n
13351335+ | None -> failwith "jsont: missing zarr_format"
13361336+ in
13371337+ match zarr_format with
13381338+ | 2 ->
13391339+ if has_shape mems then
13401340+ match Jsont.Json.decode v2_array_jsont json with
13411341+ | Ok n -> `V2 n | Error e -> failwith e
13421342+ else
13431343+ (match Jsont.Json.decode v2_group_jsont json with
13441344+ | Ok n -> `V2 n | Error e -> failwith e)
13451345+ | 3 ->
13461346+ (match Jsont.Json.decode v3_jsont json with
13471347+ | Ok n -> `V3 n | Error e -> failwith e)
13481348+ | n -> failwith (Printf.sprintf "jsont: unknown zarr_format: %d" n))
13491349+ ~enc:(function
13501350+ | `V2 node ->
13511351+ (match V2_node.kind node with
13521352+ | `Array _ ->
13531353+ (match Jsont.Json.encode v2_array_jsont node with
13541354+ | Ok j -> j | Error e -> failwith e)
13551355+ | `Group ->
13561356+ (match Jsont.Json.encode v2_group_jsont node with
13571357+ | Ok j -> j | Error e -> failwith e))
13581358+ | `V3 node ->
13591359+ (match Jsont.Json.encode v3_jsont node with
13601360+ | Ok j -> j | Error e -> failwith e))
13611361+ Jsont.json
+11
src/zarr_jsont.mli
···417417 The ["attributes"] sub-object, if present, is decoded via {!attrs_jsont} and
418418 stored in the node's [attrs] field. On encode, attributes are re-inserted
419419 as the ["attributes"] member only when non-empty. *)
420420+421421+type t = [ `V2 of V2_node.t | `V3 of V3_node.t ]
422422+(** A unified Zarr node: either a v2 node or a v3 node. *)
423423+424424+val jsont : t Jsont.t
425425+(** Top-level dispatch codec for Zarr metadata.
426426+427427+ Decodes any Zarr metadata JSON object by inspecting the ["zarr_format"]
428428+ field and dispatching to {!v2_array_jsont}, {!v2_group_jsont}, or
429429+ {!v3_jsont} as appropriate. V2 arrays are distinguished from v2 groups
430430+ by the presence of a ["shape"] field. *)
+60
test/test_zarr_jsont.ml
···457457 | None -> assert false);
458458 print_endline "test_v3_node_array_with_attrs: ok"
459459460460+let test_dispatch_v2_array () =
461461+ let json = {|{
462462+ "zarr_format": 2,
463463+ "shape": [20, 20],
464464+ "chunks": [10, 10],
465465+ "dtype": "<i4",
466466+ "compressor": {"id": "zlib", "level": 1},
467467+ "fill_value": 42,
468468+ "order": "C",
469469+ "filters": null
470470+ }|} in
471471+ let v = decode Zarr_jsont.jsont json in
472472+ (match v with
473473+ | `V2 node ->
474474+ (match Zarr_jsont.V2_node.kind node with
475475+ | `Array a -> assert (Zarr_jsont.V2.Array_meta.shape a = [20; 20])
476476+ | `Group -> assert false)
477477+ | `V3 _ -> assert false);
478478+ print_endline "test_dispatch_v2_array: ok"
479479+480480+let test_dispatch_v2_group () =
481481+ let json = {|{"zarr_format": 2}|} in
482482+ let v = decode Zarr_jsont.jsont json in
483483+ (match v with
484484+ | `V2 node -> assert (Zarr_jsont.V2_node.kind node = `Group)
485485+ | `V3 _ -> assert false);
486486+ print_endline "test_dispatch_v2_group: ok"
487487+488488+let test_dispatch_v3_array () =
489489+ let json = {|{
490490+ "zarr_format": 3,
491491+ "node_type": "array",
492492+ "shape": [100],
493493+ "data_type": "int32",
494494+ "chunk_grid": {"name": "regular", "configuration": {"chunk_shape": [10]}},
495495+ "chunk_key_encoding": {"name": "default", "configuration": {"separator": "/"}},
496496+ "codecs": [{"name": "bytes", "configuration": {"endian": "little"}}],
497497+ "fill_value": 0
498498+ }|} in
499499+ let v = decode Zarr_jsont.jsont json in
500500+ (match v with
501501+ | `V3 node ->
502502+ (match Zarr_jsont.V3_node.kind node with
503503+ | `Array a -> assert (Zarr_jsont.V3.Array_meta.data_type a = `Int32)
504504+ | `Group -> assert false)
505505+ | `V2 _ -> assert false);
506506+ print_endline "test_dispatch_v3_array: ok"
507507+508508+let test_dispatch_v3_group () =
509509+ let json = {|{"zarr_format": 3, "node_type": "group"}|} in
510510+ let v = decode Zarr_jsont.jsont json in
511511+ (match v with
512512+ | `V3 node -> assert (Zarr_jsont.V3_node.kind node = `Group)
513513+ | `V2 _ -> assert false);
514514+ print_endline "test_dispatch_v3_group: ok"
515515+460516let () = test_other_codec ()
461517let () = test_other_ext ()
462518let () = test_fill_value ()
···476532let () = test_v3_node_group ()
477533let () = test_v3_node_array ()
478534let () = test_v3_node_array_with_attrs ()
535535+let () = test_dispatch_v2_array ()
536536+let () = test_dispatch_v2_group ()
537537+let () = test_dispatch_v3_array ()
538538+let () = test_dispatch_v3_group ()