Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3799.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a `JSONSerializable` protocol in `zarr.abc.serializable`, parameterized on the type returned by `to_json`. `ArrayV3Metadata` now implements this protocol via a `to_json` method that returns an `ArrayMetadataJSON_V3` typed dictionary.
9 changes: 9 additions & 0 deletions src/zarr/abc/serializable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Protocol


class JSONSerializable[T_co](Protocol):
def to_json(self) -> T_co:
"""
Serialize to a JSON-compatible Python object.
"""
...
6 changes: 6 additions & 0 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ def to_dict(self) -> dict[str, JSON]:
out_dict["data_type"] = dtype_meta.to_json(zarr_format=3) # type: ignore[unreachable]
return out_dict

def to_json(self) -> ArrayMetadataJSON_V3:
"""
Serialize this array metadata to a JSON-compatible Python object.
"""
return cast(ArrayMetadataJSON_V3, self.to_dict())

def update_shape(self, shape: tuple[int, ...]) -> Self:
chunk_grid = self.chunk_grid
if isinstance(chunk_grid, RectilinearChunkGridMetadata):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_abc/test_serializable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from zarr.core.dtype.npy.int import UInt8
from zarr.core.metadata.v3 import ArrayV3Metadata

if TYPE_CHECKING:
from zarr.abc.serializable import JSONSerializable
from zarr.core.metadata.v3 import ArrayMetadataJSON_V3


def test_array_v3_metadata_to_json() -> None:
"""
ArrayV3Metadata satisfies the JSONSerializable protocol parameterized
on its JSON output type, and ``to_json`` returns the same payload as
``to_dict``.
"""
metadata = ArrayV3Metadata(
shape=(10,),
data_type=UInt8(),
chunk_grid={"name": "regular", "configuration": {"chunk_shape": (10,)}},
chunk_key_encoding={"name": "default", "configuration": {"separator": "/"}},
fill_value=0,
codecs=({"name": "bytes", "configuration": {"endian": "little"}},),
attributes={},
dimension_names=None,
)
serializable: JSONSerializable[ArrayMetadataJSON_V3] = metadata
assert serializable.to_json() == metadata.to_dict()
Loading