|
5 | 5 | import sys |
6 | 6 | from pathlib import Path |
7 | 7 | from datetime import date, time |
| 8 | +import platform |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class EliotJSONEncoder(json.JSONEncoder): |
@@ -90,6 +91,34 @@ def json_default(o: object) -> object: |
90 | 91 | raise TypeError("Unsupported type") |
91 | 92 |
|
92 | 93 |
|
| 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 | + |
93 | 122 | def _encoder_to_default_function( |
94 | 123 | encoder: json.JSONEncoder, |
95 | 124 | ) -> Callable[[object], object]: |
|
0 commit comments