Recursively coerce ``obj`` into a JSON-serializable structure. Unknown/opaque values (functions, dataclasses, …) degrade to ``str`` so a single bad field never makes the WS pump's ``json.dumps`` raise.
(obj: Any)
| 1737 | |
| 1738 | |
| 1739 | def _json_safe(obj: Any) -> Any: |
| 1740 | """Recursively coerce ``obj`` into a JSON-serializable structure. |
| 1741 | |
| 1742 | Unknown/opaque values (functions, dataclasses, …) degrade to ``str`` so a |
| 1743 | single bad field never makes the WS pump's ``json.dumps`` raise. |
| 1744 | """ |
| 1745 | if obj is None or isinstance(obj, (bool, int, float, str)): |
| 1746 | return obj |
| 1747 | if isinstance(obj, Mapping): |
| 1748 | return {str(k): _json_safe(v) for k, v in obj.items()} |
| 1749 | if isinstance(obj, (list, tuple)): |
| 1750 | return [_json_safe(v) for v in obj] |
| 1751 | return str(obj) |
| 1752 | |
| 1753 | |
| 1754 | def _current_mode(tool_context: Any, default: str) -> str: |