Coverage for python/lsst/daf/butler/formatters/json.py: 82%

32 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-16 02:53 -0800

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__ = ("JsonFormatter",) 

25 

26import dataclasses 

27import json 

28from typing import Any, Optional, Type 

29 

30from .file import FileFormatter 

31 

32 

33class JsonFormatter(FileFormatter): 

34 """Formatter implementation for JSON files.""" 

35 

36 extension = ".json" 

37 

38 unsupportedParameters = None 

39 """This formatter does not support any parameters (`frozenset`)""" 

40 

41 def _readFile(self, path: str, pytype: Optional[Type[Any]] = None) -> Any: 

42 """Read a file from the path in JSON format. 

43 

44 Parameters 

45 ---------- 

46 path : `str` 

47 Path to use to open JSON format file. 

48 pytype : `class`, optional 

49 Not used by this implementation. 

50 

51 Returns 

52 ------- 

53 data : `object` 

54 Data as Python object read from JSON file. 

55 """ 

56 with open(path, "rb") as fd: 

57 data = self._fromBytes(fd.read(), pytype) 

58 

59 return data 

60 

61 def _writeFile(self, inMemoryDataset: Any) -> None: 

62 """Write the in memory dataset to file on disk. 

63 

64 Will look for `_asdict()` method to aid JSON serialization, following 

65 the approach of the simplejson module. 

66 

67 Parameters 

68 ---------- 

69 inMemoryDataset : `object` 

70 Object to serialize. 

71 

72 Raises 

73 ------ 

74 Exception 

75 The file could not be written. 

76 """ 

77 self.fileDescriptor.location.uri.write(self._toBytes(inMemoryDataset)) 

78 

79 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = None) -> Any: 

80 """Read the bytes object as a python object. 

81 

82 Parameters 

83 ---------- 

84 serializedDataset : `bytes` 

85 Bytes object to unserialize. 

86 pytype : `class`, optional 

87 Not used by this implementation. 

88 

89 Returns 

90 ------- 

91 inMemoryDataset : `object` 

92 The requested data as a Python object or None if the string could 

93 not be read. 

94 """ 

95 try: 

96 data = json.loads(serializedDataset) 

97 except json.JSONDecodeError: 

98 data = None 

99 

100 return data 

101 

102 def _toBytes(self, inMemoryDataset: Any) -> bytes: 

103 """Write the in memory dataset to a bytestring. 

104 

105 Parameters 

106 ---------- 

107 inMemoryDataset : `object` 

108 Object to serialize 

109 

110 Returns 

111 ------- 

112 serializedDataset : `bytes` 

113 bytes representing the serialized dataset. 

114 

115 Raises 

116 ------ 

117 Exception 

118 The object could not be serialized. 

119 """ 

120 # For example, Pydantic models have a .json method so use it. 

121 try: 

122 return inMemoryDataset.json().encode() 

123 except AttributeError: 

124 pass 

125 

126 if dataclasses.is_dataclass(inMemoryDataset): 

127 inMemoryDataset = dataclasses.asdict(inMemoryDataset) 

128 elif hasattr(inMemoryDataset, "_asdict"): 

129 inMemoryDataset = inMemoryDataset._asdict() 

130 return json.dumps(inMemoryDataset, ensure_ascii=False).encode()