repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
Mirrored from https://github.com/yingqi-z20/Agent-libOS
stars
latest
clone command
git clone gitlawb://did:key:z6MkqRzA...RfoM/yingqi-z20-Agen...git clone gitlawb://did:key:z6MkqRzA.../yingqi-z20-Agen...d98dd2c9IPC1d ago| #1 | from __future__ import annotations |
| #2 | |
| #3 | import json |
| #4 | from collections.abc import Mapping, Sequence |
| #5 | from dataclasses import asdict, is_dataclass |
| #6 | from enum import Enum |
| #7 | from typing import Any |
| #8 | |
| #9 | |
| #10 | def to_jsonable(value: Any) -> Any: |
| #11 | if is_dataclass(value): |
| #12 | return {key: to_jsonable(item) for key, item in asdict(value).items()} |
| #13 | if isinstance(value, Enum): |
| #14 | return value.value |
| #15 | if isinstance(value, (str, int, float, bool)) or value is None: |
| #16 | return value |
| #17 | model_dump = getattr(value, "model_dump", None) |
| #18 | if callable(model_dump): |
| #19 | try: |
| #20 | return to_jsonable(model_dump(mode="json")) |
| #21 | except TypeError: |
| #22 | return to_jsonable(model_dump()) |
| #23 | to_dict = getattr(value, "to_dict", None) |
| #24 | if callable(to_dict): |
| #25 | return to_jsonable(to_dict()) |
| #26 | if isinstance(value, set): |
| #27 | return sorted(to_jsonable(item) for item in value) |
| #28 | if isinstance(value, tuple): |
| #29 | return [to_jsonable(item) for item in value] |
| #30 | if isinstance(value, list): |
| #31 | return [to_jsonable(item) for item in value] |
| #32 | if isinstance(value, Mapping): |
| #33 | return {str(key): to_jsonable(item) for key, item in value.items()} |
| #34 | if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)): |
| #35 | return [to_jsonable(item) for item in value] |
| #36 | if hasattr(value, "__dict__"): |
| #37 | return { |
| #38 | str(key): to_jsonable(item) |
| #39 | for key, item in vars(value).items() |
| #40 | if not callable(item) |
| #41 | } |
| #42 | return value |
| #43 | |
| #44 | |
| #45 | def dumps(value: Any) -> str: |
| #46 | return json.dumps(to_jsonable(value), ensure_ascii=True, sort_keys=True, default=str) |
| #47 | |
| #48 | |
| #49 | def loads(value: str | None, default: Any = None) -> Any: |
| #50 | if value is None: |
| #51 | return default |
| #52 | return json.loads(value) |
| #53 |