5
5
JSON Encoder, Decoder.
6
6
"""
7
7
import uuid
8
+ from pathlib import PosixPath, PurePath, Path
9
+ from datetime import datetime
8
10
from asyncpg.pgproto import pgproto
11
+ from psycopg2 import Binary
9
12
from dataclasses import _MISSING_TYPE, MISSING
10
13
from typing import Any, Union
11
14
from decimal import Decimal
15
+ from enum import Enum, EnumType
12
16
from ..exceptions cimport ParserError
13
17
from ..fields import Field
14
18
import orjson
@@ -18,9 +22,6 @@ cdef class JSONContent:
18
22
"""
19
23
Basic Encoder using orjson
20
24
"""
21
- # def __init__(self, **kwargs):
22
- # # eventually take into consideration when serializing
23
- # self.options = kwargs
24
25
def __call__ (self , object obj , **kwargs ):
25
26
return self .encode(obj, ** kwargs)
26
27
@@ -29,12 +30,19 @@ cdef class JSONContent:
29
30
return float (obj)
30
31
elif hasattr (obj, " isoformat" ):
31
32
return obj.isoformat()
33
+ elif isinstance (obj, datetime):
34
+ return str (obj)
32
35
elif isinstance (obj, pgproto.UUID):
33
36
return str (obj)
34
37
elif isinstance (obj, uuid.UUID):
35
38
return obj
39
+ elif isinstance (obj, (PosixPath, PurePath, Path)):
40
+ return str (obj)
36
41
elif hasattr (obj, " hex" ):
37
- return obj.hex
42
+ if isinstance (obj, bytes):
43
+ return obj.hex()
44
+ else :
45
+ return obj.hex
38
46
elif hasattr (obj, ' lower' ): # asyncPg Range:
39
47
up = obj.upper
40
48
if isinstance (up, int ):
@@ -46,9 +54,20 @@ cdef class JSONContent:
46
54
return None
47
55
elif obj is MISSING:
48
56
return None
57
+ elif isinstance (obj, (Enum, EnumType)):
58
+ if obj is None :
59
+ return None
60
+ if hasattr (obj, ' value' ):
61
+ return obj.value
62
+ else :
63
+ return obj.name
64
+ elif isinstance (obj, Binary): # Handle bytea column from PostgreSQL
65
+ return str (obj) # Convert Binary object to string
49
66
elif isinstance (obj, Field):
50
67
return obj.to_dict()
51
- raise TypeError (f" {obj!r} is not JSON serializable" )
68
+ raise TypeError (
69
+ f' {obj!r} of Type {type(obj)} is not JSON serializable'
70
+ )
52
71
53
72
def encode (self , object obj , **kwargs ) -> str:
54
73
# decode back to str , as orjson returns bytes
@@ -96,3 +115,15 @@ cpdef str json_encoder(object obj):
96
115
97
116
cpdef object json_decoder(object obj):
98
117
return JSONContent().loads(obj)
118
+
119
+
120
+ cdef class BaseEncoder:
121
+ """
122
+ Encoder replacement for json.dumps but using orjson
123
+ """
124
+ def __init__ (self , *args , **kwargs ):
125
+ # Filter/adapt JSON arguments to ORJSON ones
126
+ rjargs = ()
127
+ rjkwargs = {}
128
+ encoder = JSONContent(* rjargs, ** rjkwargs)
129
+ self .encode = encoder.__call__
0 commit comments