Coverage for python/lsst/daf/butler/core/json.py : 52%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = ("to_json_generic", "from_json_generic")
26from typing import (
27 TYPE_CHECKING,
28 Any,
29 Optional,
30 Protocol,
31 Type,
32)
34import json
36if TYPE_CHECKING: 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true
37 from .dimensions import DimensionUniverse
38 from ..registry import Registry
41class SupportsSimple(Protocol):
42 def to_simple(self, minimal: bool) -> Any:
43 ...
45 @classmethod
46 def from_simple(cls, simple: Any, universe: Optional[DimensionUniverse] = None,
47 registry: Optional[Registry] = None) -> SupportsSimple:
48 ...
51def to_json_generic(self: SupportsSimple, minimal: bool = False) -> str:
52 """Convert this class to JSON form.
54 The class type is not recorded in the JSON so the JSON decoder
55 must know which class is represented.
57 Parameters
58 ----------
59 minimal : `bool`, optional
60 Use minimal serialization. Requires Registry to convert
61 back to a full type.
63 Returns
64 -------
65 json : `str`
66 The class in JSON string format.
67 """
68 # For now use the core json library to convert a dict to JSON
69 # for us.
70 return json.dumps(self.to_simple(minimal=minimal))
73def from_json_generic(cls: Type[SupportsSimple], json_str: str,
74 universe: Optional[DimensionUniverse] = None,
75 registry: Optional[Registry] = None) -> SupportsSimple:
76 """Convert a JSON string created by `to_json` and return
77 something of the supplied class.
79 Parameters
80 ----------
81 json_str : `str`
82 Representation of the dimensions in JSON format as created
83 by `to_json()`.
84 universe : `DimensionUniverse`, optional
85 The special graph of all known dimensions. Passed directly
86 to `from_simple()`.
87 registry : `lsst.daf.butler.Registry`, optional
88 Registry to use to convert simple name of a DatasetType to
89 a full `DatasetType`. Passed directly to `from_simple()`.
91 Returns
92 -------
93 constructed : Any
94 Newly-constructed object.
95 """
96 simple = json.loads(json_str)
97 try:
98 return cls.from_simple(simple, universe=universe, registry=registry)
99 except AttributeError as e:
100 raise AttributeError(f"JSON deserialization requires {cls} has a from_simple() class method") from e