首页 > 其他分享 >[FastAPI-24]jsonable_encoder 序列化

[FastAPI-24]jsonable_encoder 序列化

时间:2023-03-25 17:58:20浏览次数:42  
标签:24 FastAPI jsonable encoder item json import 序列化

import typing
import json
from fastapi import FastAPI, Response
from fastapi.encoders import jsonable_encoder
from datetime import datetime
from pydantic import BaseModel

app = FastAPI()

'''
jsonable_encoder
数据序列化
'''

class Item(BaseModel):
    name: str
    description: typing.Union[str, None] = None
    timestamp : datetime

@app.post("/item")
def create_item(item : Item):
    print(item)
    jsonable_item = jsonable_encoder(item)
    json_item = json.dumps(jsonable_item)
    return json_item

标签:24,FastAPI,jsonable,encoder,item,json,import,序列化
From: https://www.cnblogs.com/leoshi/p/17255234.html

相关文章

  • [FastAPI-23]响应体pydantic dict方法
    importtypingfromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()'''pydanticd......
  • 宝塔上部署FastAPI的步骤和一些注意点
    为了运维方便,选择直接用宝塔来管理pythonfastapi的项目,虽然直接部署可能性能更好更灵活,但是我选择了低层本,每个人的选择可能是不一样的,各有考虑吧。 本文的大逻辑是先......
  • COMP30024 Artificial Intelligence
    ProjectPartASinglePlayerInfexionCOMP30024ArtificialIntelligenceMarch2023OverviewInthisfirstpartoftheproject,youwillwriteaprogramtoplayasi......
  • Winform/Csharp中使用StackExchange.Redis连接Redis存取数据并序列化对象/反序列化(支
    场景在winform程序中,需要连接Redis并根据Key进行模糊搜索,对value值进行反序列化为对象之后进行数据处理和显示。ServiceStack.redis这里不使用servicestack.redis,因为......
  • Ubuntu 20.04部署kubernetes 1.24
    环境准备准备工作需要在所有节点上操作,包含的过程如下:配置主机名添加/etc/hosts清空防火墙设置apt源配置时间同步关闭swap配置内核参数加载ip_vs内核模块安装Containerd安装......
  • [FastAPI-22]响应模型-response_model
    importtypingfromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()'''响应模型s......
  • [FastAPI-21]响应体
    fromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()classUser(BaseModel):u......
  • [FastAPI-20]设置响应头
    fromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()classUser(BaseModel):u......
  • 高抗干扰/抗噪LCD液晶段码屏显示驱动IC-VK2C21A/B/C/D SOP28/24/20/16
    型号:VK2C21A/B/C/D封装形式:SOP28/24/20/16可定制裸片:DICE(COB邦定片);COG(邦定玻璃用)年份:新年份 VK2C21A/B/C/D概述:VK2C21是一个点阵式存储映射的LCD驱动器,可支持最大80点(2......
  • 做题记录 230324 // 最小生成树
    为什么擦眼睛会痛因为拭目痛いA.JungleRoadshttp://222.180.160.110:1024/contest/3452/problem/1纯最小生成树,比较坑的点是因为向POJ远程提交,所以没办法用万能头,......