def custom_json_encoder(o):
"""
A custom json encoder that knows how to encode other types commonly used by Inmanta
"""
if isinstance(o, uuid.UUID):
return str(o)
if isinstance(o, datetime):
return o.isoformat()
if hasattr(o, "to_dict"):
return o.to_dict()
if isinstance(o, enum.Enum):
return o.name
if isinstance(o, Exception):
# Logs can push exceptions through RPC. Return a string representation.
return str(o)
if isinstance(o, execute.util.Unknown):
return const.UKNOWN_STRING
LOGGER.error("Unable to serialize %s", o)
raise TypeError(repr(o) + " is not JSON serializable")
评论列表
文章目录