Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions packages/traceloop-sdk/traceloop/sdk/utils/json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@ def default(self, o):
if "callbacks" in o:
del o["callbacks"]
return o
if dataclasses.is_dataclass(o):

if dataclasses.is_dataclass(o) and not isinstance(o, type):
return dataclasses.asdict(o)

if hasattr(o, "to_json"):
return o.to_json()
to_json_method = getattr(o, "to_json", None)

if callable(to_json_method):
return to_json_method()

json_method = getattr(o, "json", None)

if hasattr(o, "json"):
json_method = o.json
if callable(json_method) and not inspect.iscoroutinefunction(json_method):
if callable(json_method):
try:
result = json_method()
if not inspect.iscoroutine(result):
return result
result.close()

if inspect.iscoroutine(result):
result.close()
return type(o).__name__

return result

except Exception:
pass

if hasattr(o, "__class__"):
return o.__class__.__name__
return type(o).__name__

return super().default(o)