32 lines
No EOL
821 B
Python
32 lines
No EOL
821 B
Python
"""
|
|
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) |