diff --git a/src/aerovaldb/utils/json.py b/src/aerovaldb/utils/json.py index 375b6d6..e2df8d9 100644 --- a/src/aerovaldb/utils/json.py +++ b/src/aerovaldb/utils/json.py @@ -1,6 +1,13 @@ import simplejson # type: ignore +def json_encoder(obj): + if isinstance(obj, set): + return list(obj) + + TypeError(repr(obj) + " is not JSON serializable") + + def json_dumps_wrapper(obj, **kwargs) -> str: """ Wrapper which calls simplejson.dumps with the correct options, known to work for objects @@ -8,4 +15,4 @@ def json_dumps_wrapper(obj, **kwargs) -> str: This ensures that nan values are serialized as null to be compliant with the json standard. """ - return simplejson.dumps(obj, ignore_nan=True, **kwargs) + return simplejson.dumps(obj, ignore_nan=True, default=json_encoder, **kwargs) diff --git a/tests/test_aerovaldb.py b/tests/test_aerovaldb.py index 3b9f5ce..3243213 100644 --- a/tests/test_aerovaldb.py +++ b/tests/test_aerovaldb.py @@ -557,3 +557,19 @@ def test_put_report_image(tmpdb): assert isinstance(blob, bytes) assert len(blob) > 0 + + +@pytest.mark.parametrize( + "dbtype", + ( + pytest.param( + "json_files", + ), + pytest.param( + "sqlitedb", + ), + ), +) +def test_serialize_set(tmpdb): + with tmpdb as db: + db.put_config({"set": {"a", "b", "c"}}, "test", "test")