Skip to content

Commit 5d0ad55

Browse files
committed
allow strict="skip"
1 parent 26acf0b commit 5d0ad55

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/monty/json.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,8 @@ def jsanitize(
905905
jsanitize will try to get the as_dict() attribute of the object. If
906906
no such attribute is found, an attribute error will be thrown. If
907907
strict is False, jsanitize will simply call str(object) to convert
908-
the object to a string representation.
908+
the object to a string representation. If "skip" is provided,
909+
jsanitize will skip and return the original object without modification.
909910
allow_bson (bool): This parameter sets the behavior when jsanitize
910911
encounters a bson supported type such as objectid and datetime. If
911912
True, such bson types will be ignored, allowing for proper
@@ -1009,7 +1010,7 @@ def jsanitize(
10091010
except AttributeError:
10101011
pass
10111012

1012-
if not strict:
1013+
if strict is False:
10131014
return str(obj)
10141015

10151016
if isinstance(obj, str):
@@ -1024,13 +1025,18 @@ def jsanitize(
10241025
recursive_msonable=recursive_msonable,
10251026
)
10261027

1027-
return jsanitize(
1028-
obj.as_dict(),
1029-
strict=strict,
1030-
allow_bson=allow_bson,
1031-
enum_values=enum_values,
1032-
recursive_msonable=recursive_msonable,
1033-
)
1028+
try:
1029+
return jsanitize(
1030+
obj.as_dict(),
1031+
strict=strict,
1032+
allow_bson=allow_bson,
1033+
enum_values=enum_values,
1034+
recursive_msonable=recursive_msonable,
1035+
)
1036+
except Exception as exc_:
1037+
if strict == "skip":
1038+
return obj
1039+
raise exc_
10341040

10351041

10361042
def _serialize_callable(o):

0 commit comments

Comments
 (0)