Translates a mapping / sequence recursively in the same fashion as `pydantic` v2's `model_dump(mode="json")`.
(data: object)
| 418 | |
| 419 | |
| 420 | def 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 |
no test coverage detected