jsonify 默认做了哪些你没注意到的事?中文为什么变成 \uXXXX?datetime 会变成什么?哪些类型不能序列化?这篇把默认值和各种类型逐个打印,再换一个自定义 provider。
GET /j → application/json | '{"a":1,"b":2,"\\u4e2d\\u6587":"\\u503c"}\n'
GET /list → application/json | '[1,2,{"a":2,"z":1}]\n'
GET /one → application/json | '{"k":"v"}\n'
GET /many → application/json | '[1,2,3]\n'
GET /none → application/json | 'null\n'
app.json 类型: DefaultJSONProvider | sort_keys = True | ensure_ascii = True | compact = None | mimetype = application/json上一篇:Response 对象构造过程;下一篇:自定义 Response Class。大批量 JSON 与流式 JSON 见大批量 JSON 响应、Streaming JSON。
app.debug=True: '{\n "a": 1,\n "b": 2,\n "\\u4e2d\\u6587": "\\u503c"\n}\n' datetime → "Fri, 02 Oct 2026 12:30:45 GMT"
naive datetime → "Fri, 02 Oct 2026 12:30:45 GMT"
date → "Fri, 02 Oct 2026 00:00:00 GMT"
time → TypeError: Object of type time is not JSON serializable
UUID → "00000000-0000-0000-0000-0000000000ff"
Decimal → "1.10"
dataclass → {"x": 1, "y": "a"}
__html__ 对象 → "<b>x</b>"
Enum → TypeError: Object of type Color is not JSON serializable
set → TypeError: Object of type set is not JSON serializable
tuple → [1, 2]
bytes → TypeError: Object of type bytes is not JSON serializable
complex → TypeError: Object of type complex is not JSON serializable
普通对象 → TypeError: Object of type Plain is not JSON serializable
float nan → NaN
float inf → Infinity
大整数 → 1180591620717411303424
键是 int → {"1": "a"}
键是 tuple → TypeError: keys must be str, int, float, bool or None, not tuple 自定义 provider: {"z":1,"a":[1,2,3],"raw":"ab","obj":{"a":1}} return dict → {"a":2,"b":1} | jsonify → {"a":2,"b":1} | 完全相同: Trueapp.json.dumps(sort_keys=True,默认) 0.793 ms;sort_keys=False 0.605 ms;标准库 json.dumps 0.594 ms;标准库紧凑分隔符 0.598 ms;输出长度 73540 字节