Skip to content

Commit

Permalink
Use orjson for JSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
judahrand committed Jun 13, 2024
1 parent 8fc7497 commit 1d7ffd1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
102 changes: 49 additions & 53 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies = [
# for manipulating pyproject.toml
"tomli>=1.1.0; python_version < \"3.11\"",
"tomli-w",
"orjson>=3.10.4",
]
dynamic = ["version"]
[project.urls]
Expand Down
4 changes: 2 additions & 2 deletions src/_bentoml_impl/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import inspect
import io
import json
import logging
import mimetypes
import os
Expand All @@ -18,6 +17,7 @@

import attr
import httpx
import orjson
from pydantic import RootModel

from _bentoml_sdk import IODescriptor
Expand Down Expand Up @@ -295,7 +295,7 @@ def is_file_field(k: str) -> bool:

for name, value in fields.items():
if not is_file_field(name):
data[name] = json.dumps(value)
data[name] = orjson.dumps(value)
continue
if not isinstance(value, (list, tuple)):
value = [value]
Expand Down
10 changes: 5 additions & 5 deletions src/_bentoml_impl/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import abc
import io
import json
import pickle
import posixpath
import typing as t
from urllib.parse import unquote
from urllib.parse import urlparse

import attrs
import orjson
from pydantic import BaseModel
from starlette.datastructures import Headers
from starlette.datastructures import UploadFile
Expand Down Expand Up @@ -157,10 +157,10 @@ def deserialize_model(self, payload: Payload, cls: type[T]) -> T:
return cls.model_validate_json(b"".join(payload.data) or b"{}")

def serialize_value(self, obj: t.Any) -> Payload:
return Payload((json.dumps(obj).encode("utf-8"),))
return Payload((orjson.dumps(obj),))

def deserialize_value(self, payload: Payload) -> t.Any:
return json.loads(b"".join(payload.data) or b"{}")
return orjson.loads(b"".join(payload.data) or b"{}")


class MultipartSerde(JSONSerde):
Expand Down Expand Up @@ -199,8 +199,8 @@ async def parse_request(self, request: Request, cls: type[T]) -> T:
else:
assert isinstance(v := form[k], str)
try:
data[k] = json.loads(v)
except json.JSONDecodeError:
data[k] = orjson.loads(v)
except orjson.JSONDecodeError:
data[k] = v
return cls.model_validate(data)

Expand Down

0 comments on commit 1d7ffd1

Please sign in to comment.