Skip to content

Commit

Permalink
json output format: more robust with respect to unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
jdries committed Feb 19, 2024
1 parent 1c13f11 commit 46b9acc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 13 additions & 1 deletion openeo_driver/save_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import warnings
import logging
from datetime import datetime, date
from pathlib import Path
from shutil import copy
from tempfile import mkstemp
Expand Down Expand Up @@ -171,13 +172,24 @@ def write_assets(self, directory: Union[str, Path]) -> Dict[str, StacAsset]:
:return: STAC assets dictionary: https://github.com/radiantearth/stac-spec/blob/master/item-spec/item-spec.md#assets
"""

def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""

if isinstance(obj, (datetime, date)):
return obj.isoformat()
else:
return str(obj)



# TODO: There is something wrong here: arg is called `directory`,
# but implementation and actual usage handles it as a file path (take parent to get directory)
output_dir = Path(directory).parent
output_file = output_dir / "result.json"
with open(output_file, 'w') as f:
import json
json.dump(self.prepare_for_json(), f)
json.dump(self.prepare_for_json(), f,default=json_serial)
return {"result.json":{
"href":str(output_file),
"roles": ["data"],
Expand Down
20 changes: 19 additions & 1 deletion tests/test_save_result.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import json
from pathlib import Path

Expand All @@ -9,7 +10,7 @@
from openeo.metadata import CollectionMetadata
from openeo_driver.datacube import DriverVectorCube
from openeo_driver.save_result import AggregatePolygonResult, SaveResult, AggregatePolygonSpatialResult, \
AggregatePolygonResultCSV
AggregatePolygonResultCSV, JSONResult
from .data import load_json, json_normalize, get_path


Expand Down Expand Up @@ -172,3 +173,20 @@ def test_load_csv(self):
[4646.262612301313, 4865.926572218383, 5178.517363510712],
[4645.719597475695, 4865.467252259935, 5177.803342998465],
]

def test_jsonresult(tmp_path):

the_data = {
"bla":"bla",
"bla2":np.nan,
"bla3":{'bla':'bla'},
"bla4":datetime.datetime.fromisocalendar(2020,3,1)
}
JSONResult(the_data).write_assets(tmp_path/"bla")

the_path = tmp_path.joinpath("result.json")
actual_data = json.load(the_path.open())
assert { 'bla': 'bla',
'bla2': None,
'bla3': {'bla': 'bla'},
'bla4': '2020-01-13T00:00:00'} == actual_data

0 comments on commit 46b9acc

Please sign in to comment.