MCPcopy
hub / github.com/anthropics/anthropic-sdk-python / json_safe

Function json_safe

src/anthropic/_utils/_utils.py:420–433  ·  view source on GitHub ↗

Translates a mapping / sequence recursively in the same fashion as `pydantic` v2's `model_dump(mode="json")`.

(data: object)

Source from the content-addressed store, hash-verified

418
419
420def json_safe(data: object) -> object:
421 """Translates a mapping / sequence recursively in the same fashion
422 as `pydantic` v2's `model_dump(mode="json")`.
423 """
424 if is_mapping(data):
425 return {json_safe(key): json_safe(value) for key, value in data.items()}
426
427 if is_iterable(data) and not isinstance(data, (str, bytes, bytearray)):
428 return [json_safe(item) for item in data]
429
430 if isinstance(data, (datetime, date)):
431 return data.isoformat()
432
433 return data

Callers 1

model_dumpMethod · 0.85

Calls 2

is_mappingFunction · 0.85
is_iterableFunction · 0.85

Tested by

no test coverage detected