This commit is contained in:
Zhewen Tan 2026-09-13 00:04:05 -07:00 committed by GitHub
commit 25801ada99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -62,7 +62,10 @@ def safe_dumps(
seen.remove(id(obj))
return result
elif isinstance(obj, set):
result = sorted([_serialize(item, seen, depth + 1, key) for item in obj])
result = sorted(
[_serialize(item, seen, depth + 1, key) for item in obj],
key=lambda item: json.dumps(item, sort_keys=True, default=str),
)
seen.remove(id(obj))
return result
elif isinstance(obj, BaseModel):

View file

@ -64,6 +64,14 @@ def test_complex_types():
assert result["tuple"] == [4, 5, 6] # Tuples are converted to lists
def test_set_with_mixed_types():
data = {"metadata": {"labels": {"beta", 7, None}}}
result = json.loads(safe_dumps(data))
assert result["metadata"]["labels"] == ["beta", 7, None]
def test_unserializable_object():
# Test handling of unserializable objects
class TestClass: