我使用Fast API编写了如下代码:
class EnumTestT(Enum):
test_t_value = 0
object = {
test: test_t_value
}
enum_mapping = {
test_t_value: "Test"
}
def enum_encoder(val: EnumTestT) -> str:
return enum_mapping[val]
custom_encoder = {
EnumTestT: enum_encoder
}
@app.get("/test")
async def test_get():
return jsonable_encoder(object, custom_encoder=custom_encoder)
问题是jsonable_encoder
在默认值之后应用自定义编码器。有没有办法在默认编码器之前应用它们。因为对于Enum
和任何派生类,将报告枚举的值而不是映射的值
现在我在
json.dumps
中使用自定义编码器,如下所示:FastAPI应用程序中的响应是:
使用自定义编码器的FastAPI的问题在于,在调用了所有标准编码器之后调用了自定义编码器,并且无法覆盖该顺序
FastAPI使用编码器按类型(来自pydantic.json)对一些基本数据类型进行编码
所以我要覆盖默认的日期时间编码,就像
相关问题 更多 >
编程相关推荐