Convert a value to a JSON-serializable type. Handles numpy arrays, Path objects, torch tensors, and other common types.
(value: Any)
| 294 | |
| 295 | |
| 296 | def _make_json_serializable(value: Any) -> Any: |
| 297 | """ |
| 298 | Convert a value to a JSON-serializable type. |
| 299 | |
| 300 | Handles numpy arrays, Path objects, torch tensors, and other common types. |
| 301 | """ |
| 302 | if value is None: |
| 303 | return None |
| 304 | if isinstance(value, (str, int, float, bool)): |
| 305 | return value |
| 306 | if isinstance(value, (list, tuple)): |
| 307 | return [_make_json_serializable(v) for v in value] |
| 308 | if isinstance(value, dict): |
| 309 | return {str(k): _make_json_serializable(v) for k, v in value.items()} |
| 310 | if isinstance(value, np.ndarray): |
| 311 | return value.tolist() |
| 312 | if isinstance(value, (np.integer, np.floating)): |
| 313 | return value.item() |
| 314 | if isinstance(value, torch.Tensor): |
| 315 | return value.detach().cpu().numpy().tolist() |
| 316 | # Fallback to string representation |
| 317 | return str(value) |
| 318 | |
| 319 | |
| 320 | def _add_path_with_parent(paths: list[str], path: PathLike | None) -> None: |
no outgoing calls
searching dependent graphs…