# Path: il2fb/ds/airbridge/structures.py
# class TimestampedData(BaseStructure):
#     __slots__ = ['timestamp', 'data', ]
# 
#     def __init__(self, data: Any):
#         self.timestamp = datetime.datetime.utcnow()
#         self.data = data
# 
#     def __repr__(self):
#         return (
#             "<TimestampedData {0}@{1}>"
#             .format(
#                 repr(self.data),
#                 self.timestamp.isoformat(),
#             )
#         )


import json as _json
from functools import partial
from il2fb.commons.events import Event
from il2fb.ds.airbridge.structures import TimestampedData
# -*- coding: utf-8 -*-






__all__ = ('dumps', 'loads', )


class JSONEncoder(_json.JSONEncoder):

    def default(self, obj):
        cls = type(obj)

        if issubclass(cls, TimestampedData):
            result = {
                key: getattr(obj, key)
                for key in cls.__slots__
            }
        elif hasattr(obj, 'to_primitive'):
            result = obj.to_primitive()
            result['__type__'] = f"{cls.__module__}.{cls.__name__}"

            if issubclass(cls, Event):
                result.pop('name')
                result.pop('verbose_name')

        elif hasattr(obj, 'isoformat'):

# Path: il2fb/ds/airbridge/structures.py
# class TimestampedData(BaseStructure):
#     __slots__ = ['timestamp', 'data', ]
# 
#     def __init__(self, data: Any):
#         self.timestamp = datetime.datetime.utcnow()
#         self.data = data
# 
#     def __repr__(self):
#         return (
#             "<TimestampedData {0}@{1}>"
#             .format(
#                 repr(self.data),
#                 self.timestamp.isoformat(),
#             )
#         )


# Path: il2fb/ds/airbridge/json.py
import json as _json
from functools import partial
from il2fb.commons.events import Event
from il2fb.ds.airbridge.structures import TimestampedData

# -*- coding: utf-8 -*-






__all__ = ('dumps', 'loads', )


class JSONEncoder(_json.JSONEncoder):

    def default(self, obj):
        cls = type(obj)

        if issubclass(cls, TimestampedData):
            result = {
                key: getattr(obj, key)
                for key in cls.__slots__
            }
        elif hasattr(obj, 'to_primitive'):
            result = obj.to_primitive()
            result['__type__'] = f"{cls.__module__}.{cls.__name__}"

            if issubclass(cls, Event):
                result.pop('name')
                result.pop('verbose_name')

        elif hasattr(obj, 'isoformat'):

            result = obj.isoformat()