18
18
from urllib .error import HTTPError
19
19
20
20
import pystac
21
- from pystac .utils import safe_urlparse
21
+ from pystac .utils import safe_urlparse , get_opt
22
22
from pystac .serialization import (
23
23
merge_common_properties ,
24
24
identify_stac_object_type ,
@@ -78,17 +78,40 @@ def write_text(
78
78
"""
79
79
raise NotImplementedError ("write_text not implemented" )
80
80
81
- def _json_loads (self , txt : str , source : Union [str , "Link_Type" ]) -> Dict [str , Any ]:
81
+ def json_loads (self , txt : str , * args : Any , ** kwargs : Any ) -> Dict [str , Any ]:
82
+ """Method used internally by :class:`StacIO` instances to deserialize a
83
+ dictionary from a JSON string.
84
+
85
+ This method may be overwritten in :class:`StacIO` sub-classes to provide custom
86
+ deserialization logic. The method accepts arbitrary keyword arguments. These are
87
+ not used by the default implementation, but may be used by sub-class
88
+ implementations.
89
+
90
+ Args:
91
+
92
+ txt : The JSON string to deserialize to a dictionary.
93
+ """
82
94
result : Dict [str , Any ]
83
95
if orjson is not None :
84
96
result = orjson .loads (txt )
85
97
else :
86
98
result = json .loads (txt )
87
99
return result
88
100
89
- def _json_dumps (
90
- self , json_dict : Dict [str , Any ], source : Union [str , "Link_Type" ]
91
- ) -> str :
101
+ def json_dumps (self , json_dict : Dict [str , Any ], * args : Any , ** kwargs : Any ) -> str :
102
+ """Method used internally by :class:`StacIO` instances to serialize a dictionary
103
+ to a JSON string.
104
+
105
+ This method may be overwritten in :class:`StacIO` sub-classes to provide custom
106
+ serialization logic. The method accepts arbitrary keyword arguments. These are
107
+ not used by the default implementation, but may be used by sub-class
108
+ implementations (see :meth:`DuplicateKeyReportingMixin.json_dumps` as an
109
+ example).
110
+
111
+ Args:
112
+
113
+ json_dict : The dictionary to serialize
114
+ """
92
115
if orjson is not None :
93
116
return orjson .dumps (json_dict , option = orjson .OPT_INDENT_2 ).decode ("utf-8" )
94
117
else :
@@ -143,16 +166,24 @@ def read_json(
143
166
144
167
Args:
145
168
source : The source from which to read.
169
+ *args : Additional positional arguments to be passed to
170
+ :meth:`StacIO.read_text`.
171
+ **kwargs : Additional keyword arguments to be passed to
172
+ :meth:`StacIO.read_text`.
146
173
147
174
Returns:
148
175
dict: A dict representation of the JSON contained in the file at the
149
176
given source.
150
177
"""
151
178
txt = self .read_text (source , * args , ** kwargs )
152
- return self ._json_loads (txt , source )
179
+ return self .json_loads (txt )
153
180
154
181
def read_stac_object (
155
- self , source : Union [str , "Link_Type" ], root : Optional ["Catalog_Type" ] = None
182
+ self ,
183
+ source : Union [str , "Link_Type" ],
184
+ root : Optional ["Catalog_Type" ] = None ,
185
+ * args : Any ,
186
+ ** kwargs : Any ,
156
187
) -> "STACObject_Type" :
157
188
"""Read a STACObject from a JSON file at the given source.
158
189
@@ -164,17 +195,25 @@ def read_stac_object(
164
195
root : Optional root of the catalog for this object.
165
196
If provided, the root's resolved object cache can be used to search for
166
197
previously resolved instances of the STAC object.
198
+ *args : Additional positional arguments to be passed to
199
+ :meth:`StacIO.read_json`.
200
+ **kwargs : Additional keyword arguments to be passed to
201
+ :meth:`StacIO.read_json`.
167
202
168
203
Returns:
169
204
STACObject: The deserialized STACObject from the serialized JSON
170
205
contained in the file at the given uri.
171
206
"""
172
- d = self .read_json (source )
207
+ d = self .read_json (source , * args , ** kwargs )
173
208
href = source if isinstance (source , str ) else source .get_absolute_href ()
174
209
return self .stac_object_from_dict (d , href = href , root = root , preserve_dict = False )
175
210
176
211
def save_json (
177
- self , dest : Union [str , "Link_Type" ], json_dict : Dict [str , Any ]
212
+ self ,
213
+ dest : Union [str , "Link_Type" ],
214
+ json_dict : Dict [str , Any ],
215
+ * args : Any ,
216
+ ** kwargs : Any ,
178
217
) -> None :
179
218
"""Write a dict to the given URI as JSON.
180
219
@@ -184,8 +223,12 @@ def save_json(
184
223
Args:
185
224
dest : The destination file to write the text to.
186
225
json_dict : The JSON dict to write.
226
+ *args : Additional positional arguments to be passed to
227
+ :meth:`StacIO.json_dumps`.
228
+ **kwargs : Additional keyword arguments to be passed to
229
+ :meth:`StacIO.json_dumps`.
187
230
"""
188
- txt = self ._json_dumps (json_dict , dest )
231
+ txt = self .json_dumps (json_dict , * args , ** kwargs )
189
232
self .write_text (dest , txt )
190
233
191
234
@classmethod
@@ -261,7 +304,8 @@ class DuplicateKeyReportingMixin(StacIO):
261
304
See https://github.com/stac-utils/pystac/issues/313
262
305
"""
263
306
264
- def _json_loads (self , txt : str , source : Union [str , "Link_Type" ]) -> Dict [str , Any ]:
307
+ def json_loads (self , txt : str , * args : Any , ** kwargs : Any ) -> Dict [str , Any ]:
308
+ source : Union [str , "Link_Type" ] = get_opt (kwargs .get ("source" ))
265
309
result : Dict [str , Any ] = json .loads (
266
310
txt , object_pairs_hook = self .duplicate_object_names_report_builder (source )
267
311
)
0 commit comments