Skip to content

Commit e9ba422

Browse files
committed
PyPy support.
1 parent 5fd8d0a commit e9ba422

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

eliot/json.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
from pathlib import Path
77
from datetime import date, time
8+
import platform
89

910

1011
class EliotJSONEncoder(json.JSONEncoder):
@@ -90,6 +91,34 @@ def json_default(o: object) -> object:
9091
raise TypeError("Unsupported type")
9192

9293

94+
if platform.python_implementation() == "PyPy":
95+
# We're not using orjson, so need to serialize a few more types.
96+
97+
original_json_default = json_default
98+
99+
def json_default(o: object, original_json_default=original_json_default) -> object:
100+
from datetime import datetime
101+
from enum import Enum
102+
from uuid import UUID
103+
104+
# Add dataclass support
105+
if hasattr(o, "__dataclass_fields__"):
106+
return {field: getattr(o, field) for field in o.__dataclass_fields__}
107+
if isinstance(o, datetime):
108+
return o.isoformat()
109+
110+
if isinstance(o, UUID):
111+
return str(o)
112+
113+
if isinstance(o, Enum):
114+
return o.value
115+
116+
return original_json_default(o)
117+
118+
json_default.__doc__ = original_json_default.__doc__
119+
del original_json_default
120+
121+
93122
def _encoder_to_default_function(
94123
encoder: json.JSONEncoder,
95124
) -> Callable[[object], object]:

0 commit comments

Comments
 (0)