我有以下 pydantic 模型。
class SubModel(BaseModel):
columns: Mapping
key: List[str]
required: Optional[List[str]]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
class MyModel(BaseModel):
name: str
config1: Optional[SubModel]
config2: Optional[Mapping]
class Config:
anystr_strip_whitespace: True
extra: Extra.allow
allow_population_by_field_name: True
Run Code Online (Sandbox Code Playgroud)
当我尝试dumps对此进行操作时,我得到了model is not JSON serializable
from io import BytesIO
from orjson import dumps
bucket = s3.Bucket(bucket_name)
bucket.upload(BytesIO(dumps(data)), key, ExtraArgs={'ContentType': 'application/json'})
Run Code Online (Sandbox Code Playgroud)
错误 -
TypeError: Type is not JSON serializable: MyModel
Run Code Online (Sandbox Code Playgroud)
data是一个普通的Python字典,其中有一个类型为item的项目MyModel。尝试使用.json()但得到dict has no attribute json。
我被困在这里了。有人能帮我吗。
我已经切换到 orjson 因为它更快,但这导致了我已经有相当长一段时间但从未考虑过的问题。我最终决定进行测试,这些就是我的测试。
\nimport orjson, json\n\n\ndata = json.dumps({"channel_id" : None, "payment_source_id" : None})\nprint(data)\n\ndata = orjson.dumps({"channel_id" : None, "payment_source_id" : None}).decode("utf-8")\nprint(data)\n\n{"channel_id": null, "payment_source_id": null}\n{"channel_id":null,"payment_source_id":null}\nRun Code Online (Sandbox Code Playgroud)\n这是我的测试文件。当您运行此命令时,您会发现唯一的区别是 null 和引号之间的空格。当我尝试使用 orjson 转储 json 数据并将其发送到请求中时,我收到 400 错误请求,有时根本没有返回任何内容,但是当尝试使用 json 库时一切正常,我收到有效的响应。我\xe2\x80\x99m 不知道该怎么做,因为就像我说的,唯一的区别是空格。有没有人遇到类似的问题,可以告诉我发生了什么\xe2\x80\x99s 或我做错了什么?另外要注意的是,如果我的代码中没有 \xe2\x80\x9cNone\xe2\x80\x9d ,则 orjson 工作正常。
\n您好,我正在尝试安装fastapi ,但在安装orjson时似乎失败
\n\n这是完整的错误代码:
\n\nBuilding wheel for orjson (PEP 517) ... error\n ERROR: Command errored out with exit status 1:\n command: \'d:\\pythonapps\\fastapi_tutorial\\env\\scripts\\python.exe\' \'d:\\pythonapps\\fastapi_tutorial\\env\\lib\\site-packages\\pip\\_vendor\\pep517\\_in_process.py\' build_wheel \'C:\\Users\\User\\AppData\\Local\\Temp\\tmp4yope0de\'\n cwd: C:\\Users\\User\\AppData\\Local\\Temp\\pip-install-35a1h4do\\orjson\n Complete output (50 lines):\n Compiling cfg-if v0.1.10\n Compiling winapi v0.3.8\n Compiling bitflags v1.2.1\n Compiling ryu v1.0.5\n Compiling scopeguard v1.1.0\n Compiling packed_simd v0.3.3\n Compiling serde v1.0.111\n Compiling lexical-core v0.7.4\n Compiling libc v0.2.71\n Compiling version_check v0.9.2\n Compiling smallvec v1.4.0\n Compiling arrayvec v0.5.1\n Compiling encoding_rs v0.8.23\n Compiling static_assertions v1.1.0\n Compiling itoa v0.4.5\n …Run Code Online (Sandbox Code Playgroud) orjson==3.3.0我正在尝试在装有 Apple M1 Pro 芯片、运行 macOS Monterey 12.2.1 的 MacBook Pro 上安装。
Python版本:3.8.9
使用的命令:pip install orjson==3.3.0
错误:
\nCollecting orjson==3.3.0\n Downloading orjson-3.3.0.tar.gz (654 kB)\n \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81 654.9/654.9 KB 2.9 MB/s eta 0:00:00\n Installing build dependencies ... error\n error: subprocess-exited-with-error\n \n \xc3\x97 pip subprocess to install build dependencies did not run successfully.\n \xe2\x94\x82 exit code: 1\n \xe2\x95\xb0\xe2\x94\x80> [468 lines of output]\n Collecting maturin<0.9,>=0.8.1\n Downloading maturin-0.8.3.tar.gz (82 kB)\n \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81 82.2/82.2 KB 1.5 MB/s eta 0:00:00\n Installing build …Run Code Online (Sandbox Code Playgroud)