diff --git a/virtualizarr/vendor/zarr/LICENSE.txt b/virtualizarr/vendor/zarr/LICENSE.txt new file mode 100644 index 00000000..a4de1c39 --- /dev/null +++ b/virtualizarr/vendor/zarr/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015-2024 Zarr Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/virtualizarr/vendor/zarr/__init__.py b/virtualizarr/vendor/zarr/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/virtualizarr/vendor/zarr/utils.py b/virtualizarr/vendor/zarr/utils.py new file mode 100644 index 00000000..32986793 --- /dev/null +++ b/virtualizarr/vendor/zarr/utils.py @@ -0,0 +1,26 @@ +import json +import numbers +from typing import Any + + +class NumberEncoder(json.JSONEncoder): + def default(self, o): + # See json.JSONEncoder.default docstring for explanation + # This is necessary to encode numpy dtype + if isinstance(o, numbers.Integral): + return int(o) + if isinstance(o, numbers.Real): + return float(o) + return json.JSONEncoder.default(self, o) + + +def json_dumps(o: Any) -> bytes: + """Write JSON in a consistent, human-readable way.""" + return json.dumps( + o, + indent=4, + sort_keys=True, + ensure_ascii=True, + separators=(",", ": "), + cls=NumberEncoder, + ).encode("ascii") diff --git a/virtualizarr/zarr.py b/virtualizarr/zarr.py index f2f15bcb..545a86fc 100644 --- a/virtualizarr/zarr.py +++ b/virtualizarr/zarr.py @@ -12,7 +12,8 @@ import ujson # type: ignore import xarray as xr from pydantic import BaseModel, ConfigDict, field_validator -from zarr.v2.util import json_dumps + +from virtualizarr.vendor.zarr.utils import json_dumps if TYPE_CHECKING: pass