import datetime
import typing as T

from flask import json

class JSONEncoder(json.JSONEncoder):
    """
    prepares job and DB objects to be json encoded
    """

    def default(self, o: T.Any) -> T.Any:

        if hasattr(o, "serialize") and callable(o.serialize):
            return o.serialize()

        if isinstance(o, datetime.datetime):
            return str(o)

        if isinstance(o, dict):
            return o

        return o.__dict__.copy()
