Skip to content

Commit 9245d94

Browse files
committed
Make json methods "public", use args/kwargs
1 parent 13a9fad commit 9245d94

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

pystac/stac_io.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from urllib.error import HTTPError
1919

2020
import pystac
21-
from pystac.utils import safe_urlparse
21+
from pystac.utils import safe_urlparse, get_opt
2222
from pystac.serialization import (
2323
merge_common_properties,
2424
identify_stac_object_type,
@@ -78,17 +78,40 @@ def write_text(
7878
"""
7979
raise NotImplementedError("write_text not implemented")
8080

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+
"""
8294
result: Dict[str, Any]
8395
if orjson is not None:
8496
result = orjson.loads(txt)
8597
else:
8698
result = json.loads(txt)
8799
return result
88100

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+
"""
92115
if orjson is not None:
93116
return orjson.dumps(json_dict, option=orjson.OPT_INDENT_2).decode("utf-8")
94117
else:
@@ -143,16 +166,24 @@ def read_json(
143166
144167
Args:
145168
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`.
146173
147174
Returns:
148175
dict: A dict representation of the JSON contained in the file at the
149176
given source.
150177
"""
151178
txt = self.read_text(source, *args, **kwargs)
152-
return self._json_loads(txt, source)
179+
return self.json_loads(txt)
153180

154181
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,
156187
) -> "STACObject_Type":
157188
"""Read a STACObject from a JSON file at the given source.
158189
@@ -164,17 +195,25 @@ def read_stac_object(
164195
root : Optional root of the catalog for this object.
165196
If provided, the root's resolved object cache can be used to search for
166197
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`.
167202
168203
Returns:
169204
STACObject: The deserialized STACObject from the serialized JSON
170205
contained in the file at the given uri.
171206
"""
172-
d = self.read_json(source)
207+
d = self.read_json(source, *args, **kwargs)
173208
href = source if isinstance(source, str) else source.get_absolute_href()
174209
return self.stac_object_from_dict(d, href=href, root=root, preserve_dict=False)
175210

176211
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,
178217
) -> None:
179218
"""Write a dict to the given URI as JSON.
180219
@@ -184,8 +223,12 @@ def save_json(
184223
Args:
185224
dest : The destination file to write the text to.
186225
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`.
187230
"""
188-
txt = self._json_dumps(json_dict, dest)
231+
txt = self.json_dumps(json_dict, *args, **kwargs)
189232
self.write_text(dest, txt)
190233

191234
@classmethod
@@ -261,7 +304,8 @@ class DuplicateKeyReportingMixin(StacIO):
261304
See https://github.com/stac-utils/pystac/issues/313
262305
"""
263306

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"))
265309
result: Dict[str, Any] = json.loads(
266310
txt, object_pairs_hook=self.duplicate_object_names_report_builder(source)
267311
)

0 commit comments

Comments
 (0)