from typing import Any

from flask.json import JSONEncoder as Base

from pycs.database.util.JSONEncoder import JSONEncoder as Database
from pycs.jobs.util.JSONEncoder import JSONEncoder as Jobs


class JSONEncoder(Base):
    """
    prepares job objects to be json encoded
    """

    def default(self, o: Any) -> Any:
        module = o.__class__.__module__

        if module.startswith('pycs.database'):
            return Database().default(o)
        if module.startswith('pycs.jobs'):
            return Jobs().default(o)

        return o.__dict__
