diff --git a/monty/json.py b/monty/json.py index 97a7bb90a..3b35e148a 100644 --- a/monty/json.py +++ b/monty/json.py @@ -715,7 +715,13 @@ def jsanitize( pass if recursive_msonable and isinstance(obj, MSONable): - return obj.as_dict() + return jsanitize( + obj.as_dict(), + strict=strict, + allow_bson=allow_bson, + enum_values=enum_values, + recursive_msonable=recursive_msonable, + ) if not strict: return str(obj) diff --git a/tests/test_json.py b/tests/test_json.py index 16ba2d6c4..116b864cc 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -15,7 +15,7 @@ from monty.json import MontyDecoder, MontyEncoder, MSONable, _load_redirect, jsanitize -from . import __version__ as tests_version +from monty import __version__ as tests_version test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files") @@ -576,6 +576,18 @@ def test_jsanitize(self): assert clean_recursive_msonable["hello"]["a"] == 1 assert clean_recursive_msonable["hello"]["b"] == 2 + DoubleGoodMSONClass = GoodMSONClass(1, 2, 3) + DoubleGoodMSONClass.values = [GoodMSONClass(1, 2, 3)] + clean_recursive_msonable = jsanitize( + DoubleGoodMSONClass, recursive_msonable=True + ) + assert clean_recursive_msonable["a"] == 1 + assert clean_recursive_msonable["b"] == 2 + assert clean_recursive_msonable["c"] == 3 + assert clean_recursive_msonable["values"][0]["a"] == 1 + assert clean_recursive_msonable["values"][0]["b"] == 2 + assert clean_recursive_msonable["values"][0]["c"] == 3 + d = {"dt": datetime.datetime.now()} clean = jsanitize(d) assert isinstance(clean["dt"], str)