Source code for langchain_core.load.dump
import json
from typing import Any, Dict
from langchain_core.load.serializable import Serializable, to_json_not_implemented
[docs]def default(obj: Any) -> Any:
"""返回一个Serializable对象或SerializedNotImplemented对象的默认值。
"""
if isinstance(obj, Serializable):
return obj.to_json()
else:
return to_json_not_implemented(obj)
[docs]def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str:
"""返回一个对象的JSON字符串表示形式。"""
if "default" in kwargs:
raise ValueError("`default` should not be passed to dumps")
try:
if pretty:
indent = kwargs.pop("indent", 2)
return json.dumps(obj, default=default, indent=indent, **kwargs)
else:
return json.dumps(obj, default=default, **kwargs)
except TypeError:
if pretty:
return json.dumps(to_json_not_implemented(obj), indent=indent, **kwargs)
else:
return json.dumps(to_json_not_implemented(obj), **kwargs)
[docs]def dumpd(obj: Any) -> Dict[str, Any]:
"""返回一个对象的json字典表示。"""
return json.loads(dumps(obj))