Hide keyboard shortcuts

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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("to_json_generic", "from_json_generic") 

25 

26from typing import ( 

27 TYPE_CHECKING, 

28 Any, 

29 Optional, 

30 Protocol, 

31 Type, 

32) 

33 

34import json 

35 

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 

39 

40 

41class SupportsSimple(Protocol): 

42 def to_simple(self, minimal: bool) -> Any: 

43 ... 

44 

45 @classmethod 

46 def from_simple(cls, simple: Any, universe: Optional[DimensionUniverse] = None, 

47 registry: Optional[Registry] = None) -> SupportsSimple: 

48 ... 

49 

50 

51def to_json_generic(self: SupportsSimple, minimal: bool = False) -> str: 

52 """Convert this class to JSON form. 

53 

54 The class type is not recorded in the JSON so the JSON decoder 

55 must know which class is represented. 

56 

57 Parameters 

58 ---------- 

59 minimal : `bool`, optional 

60 Use minimal serialization. Requires Registry to convert 

61 back to a full type. 

62 

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)) 

71 

72 

73def from_json_generic(cls: Type[SupportsSimple], json_str: str, 

74 universe: Optional[DimensionUniverse] = None, 

75 registry: Optional[Registry] = None) -> SupportsSimple: 

76 """Return new class from JSON string. 

77 

78 Converts a JSON string created by `to_json` and return 

79 something of the supplied class. 

80 

81 Parameters 

82 ---------- 

83 json_str : `str` 

84 Representation of the dimensions in JSON format as created 

85 by `to_json()`. 

86 universe : `DimensionUniverse`, optional 

87 The special graph of all known dimensions. Passed directly 

88 to `from_simple()`. 

89 registry : `lsst.daf.butler.Registry`, optional 

90 Registry to use to convert simple name of a DatasetType to 

91 a full `DatasetType`. Passed directly to `from_simple()`. 

92 

93 Returns 

94 ------- 

95 constructed : Any 

96 Newly-constructed object. 

97 """ 

98 simple = json.loads(json_str) 

99 try: 

100 return cls.from_simple(simple, universe=universe, registry=registry) 

101 except AttributeError as e: 

102 raise AttributeError(f"JSON deserialization requires {cls} has a from_simple() class method") from e