Skip to content

Commit 7153c31

Browse files
author
Mike
committed
Allow loading from null JSON files
Update requirements.txt
1 parent 2b358da commit 7153c31

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ckanapi==4.7
22
defopt==6.3.0
33
email_validator==1.2.1
4-
hdx-python-country==3.2.4
4+
hdx-python-country==3.2.5
55
ndg-httpsclient==0.5.1
66
pyasn1==0.4.8
77
pyOpenSSL==22.0.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ install_requires =
4545
ckanapi >= 4.7
4646
defopt
4747
email_validator
48-
hdx-python-country>=3.2.4
48+
hdx-python-country>=3.2.5
4949
ndg-httpsclient
5050
pyasn1
5151
pyOpenSSL

src/hdx/data/dataset.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,22 @@ def save_to_json(self, path: str):
196196
save_json(self.get_dataset_dict(), path)
197197

198198
@staticmethod
199-
def load_from_json(path: str) -> "Dataset":
199+
def load_from_json(path: str) -> Optional["Dataset"]:
200200
"""Load dataset from JSON
201201
202202
Args:
203203
path (str): Path to load dataset
204204
205205
Returns:
206-
Dataset: Dataset created from JSON
206+
Optional[Dataset]: Dataset created from JSON or None
207207
"""
208-
dataset = Dataset(load_json(path))
209-
dataset.separate_resources()
210-
return dataset
208+
with open(path) as f:
209+
jsonobj = json.loads(f.read())
210+
if jsonobj is None:
211+
return None
212+
dataset = Dataset(jsonobj)
213+
dataset.separate_resources()
214+
return dataset
211215

212216
def init_resources(self) -> None:
213217
"""Initialise self.resources list

tests/hdx/data/test_dataset_noncore.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from hdx.utilities.dateparse import parse_date_range
1111
from hdx.utilities.downloader import Download
1212
from hdx.utilities.path import temp_dir
13+
from hdx.utilities.saver import save_text
1314

1415
from hdx.api.configuration import Configuration
1516
from hdx.data.dataset import Dataset
@@ -1719,3 +1720,7 @@ def test_load_save_to_json(self, vocabulary_read):
17191720
)
17201721
assert dataset.get_tags() == tags
17211722
assert dataset.get_resource()["name"] == resource_name
1723+
1724+
save_text("null", path)
1725+
dataset = Dataset.load_from_json(path)
1726+
assert dataset is None

0 commit comments

Comments
 (0)