-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriendlyjson.py
More file actions
24 lines (21 loc) · 874 Bytes
/
friendlyjson.py
File metadata and controls
24 lines (21 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""" a friendly json encoder that can handle dataclasses, enums, datetimes, bytes, and sets."""
import json
import dataclasses
import enum
from datetime import datetime
class Encoder(json.JSONEncoder):
""" a friendly json encoder that can handle dataclasses, enums, datetimes, bytes, and sets."""
def default(self, obj):
if dataclasses.is_dataclass(obj):
return dataclasses.asdict(obj)
if isinstance(obj, enum.Enum):
return obj.name
if isinstance(obj, datetime):
return obj.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4]+"Z"
if isinstance(obj, bytes):
return obj.decode("utf-8")
if isinstance(obj, set):
return list(obj)
if isinstance(obj, Exception):
return str(obj)
return super().default(obj)