
import json as _json

from functools import partial

from il2fb.commons.events import Event


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


class JSONEncoder(_json.JSONEncoder):

    def default(self, obj):
        if hasattr(obj, 'to_primitive'):
            cls = obj.__class__

            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()
        else:
            result = super().default(obj)

        return result


def object_decoder(obj):
    return obj


dumps = partial(_json.dumps, cls=JSONEncoder)
loads = partial(_json.loads, object_hook=object_decoder)


import json as _json

from functools import partial

from il2fb.commons.events import Event

from il2fb.ds.airbridge.structures import TimestampedData


__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()
        else:
            result = super().default(obj)

        return result


def object_decoder(obj):
    return obj


dumps = partial(_json.dumps, cls=JSONEncoder)
loads = partial(_json.loads, object_hook=object_decoder)

Update custom JSONEncoder to treat TimestampedData as dict
Update custom JSONEncoder to treat TimestampedData as dict


  
  import json as _json
  
  from functools import partial
  
  from il2fb.commons.events import Event
+ 
+ from il2fb.ds.airbridge.structures import TimestampedData
  
  
  __all__ = ('dumps', 'loads', )
  
  
  class JSONEncoder(_json.JSONEncoder):
  
      def default(self, obj):
+         cls = type(obj)
-         if hasattr(obj, 'to_primitive'):
-             cls = obj.__class__
  
+         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()
          else:
              result = super().default(obj)
  
          return result
  
  
  def object_decoder(obj):
      return obj
  
  
  dumps = partial(_json.dumps, cls=JSONEncoder)
  loads = partial(_json.loads, object_hook=object_decoder)
  
Update custom JSONEncoder to treat TimestampedData as dict
## Code Before:

import json as _json

from functools import partial

from il2fb.commons.events import Event


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


class JSONEncoder(_json.JSONEncoder):

    def default(self, obj):
        if hasattr(obj, 'to_primitive'):
            cls = obj.__class__

            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()
        else:
            result = super().default(obj)

        return result


def object_decoder(obj):
    return obj


dumps = partial(_json.dumps, cls=JSONEncoder)
loads = partial(_json.loads, object_hook=object_decoder)

## Instruction:
Update custom JSONEncoder to treat TimestampedData as dict
## Code After:

import json as _json

from functools import partial

from il2fb.commons.events import Event

from il2fb.ds.airbridge.structures import TimestampedData


__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()
        else:
            result = super().default(obj)

        return result


def object_decoder(obj):
    return obj


dumps = partial(_json.dumps, cls=JSONEncoder)
loads = partial(_json.loads, object_hook=object_decoder)
