oedb-backend/oedb/utils/serialization.py

32 lines
821 B
Python
Raw Normal View History

2025-09-15 23:25:11 +02:00
"""
Serialization utilities for the OpenEventDatabase.
Provides JSON serialization functionality for event data.
"""
import json
from datetime import datetime
class EventEncoder(json.JSONEncoder):
"""
Custom JSON encoder for event data.
Handles datetime objects by converting them to ISO format strings.
"""
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
try:
return super().default(o)
except TypeError:
return str(o)
def dumps(data):
"""
Serialize data to JSON using the EventEncoder.
Args:
data: The data to serialize.
Returns:
str: The JSON string representation of the data.
"""
return json.dumps(data, cls=EventEncoder, sort_keys=True, ensure_ascii=False)