Skip to content

Commit

Permalink
refactor: move serialization helpers into one place (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
guacs committed Sep 29, 2023
1 parent 3126ac6 commit d2410a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
24 changes: 24 additions & 0 deletions advanced_alchemy/_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Any

__all__ = ("encode_json", "decode_json")

try:
from msgspec.json import Decoder, Encoder

encoder, decoder = Encoder(), Decoder()
decode_json = decoder.decode

def encode_json(data: Any) -> str:
return encoder.encode(data).decode("utf-8")

except ImportError:
try:
from orjson import dumps as _encode_json
from orjson import loads as decode_json # type: ignore[no-redef]

def encode_json(data: Any) -> str:
return _encode_json(data).decode("utf-8") # type: ignore[no-any-return]

except ImportError:
from json import dumps as encode_json # type: ignore[assignment]
from json import loads as decode_json # type: ignore[assignment]
21 changes: 1 addition & 20 deletions advanced_alchemy/config/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Callable, Literal

from advanced_alchemy._serialization import decode_json, encode_json
from advanced_alchemy.config.types import Empty

if TYPE_CHECKING:
Expand All @@ -14,26 +15,6 @@

from advanced_alchemy.config.types import EmptyType

try:
from msgspec.json import Decoder, Encoder

encoder, decoder = Encoder(), Decoder()
decode_json = decoder.decode

def encode_json(data: Any) -> str:
return encoder.encode(data).decode("utf-8")

except ImportError:
try:
from orjson import dumps as _encode_json
from orjson import loads as decode_json # type: ignore[no-redef]

def encode_json(data: Any) -> str:
return _encode_json(data).decode("utf-8") # type: ignore[no-any-return]

except ImportError:
from json import dumps as encode_json # type: ignore[assignment]
from json import loads as decode_json # type: ignore[assignment]

__all__ = ("EngineConfig",)

Expand Down
4 changes: 2 additions & 2 deletions advanced_alchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from base64 import b64decode
from typing import TYPE_CHECKING, Any, cast

from msgspec.json import decode as decode_json
from msgspec.json import encode as encode_json
from sqlalchemy import DateTime, text, util
from sqlalchemy.dialects.oracle import BLOB as ORA_BLOB
from sqlalchemy.dialects.oracle import RAW as ORA_RAW
Expand All @@ -15,6 +13,8 @@
from sqlalchemy.types import BINARY, CHAR, BigInteger, Integer, SchemaType, TypeDecorator, TypeEngine
from sqlalchemy.types import JSON as _JSON

from advanced_alchemy._serialization import decode_json, encode_json

if TYPE_CHECKING:
from sqlalchemy.engine import Dialect

Expand Down

0 comments on commit d2410a6

Please sign in to comment.