Skip to content

Allow configuring JSON encoder for job.get_meta() serialization in web.py #510

@Flix6x

Description

@Flix6x

Problem

Viewing the job page leads to a server error if the job.meta contains objects that are not serializable by the default JSONEncoder.

Currently, rq-dashboard serializes job metadata like this (in web.py):

json.dumps(job.get_meta())

This uses the default JSON encoder. In some cases, job metadata may contain types that aren’t JSON-serializable by default (e.g., datetime.datetime, datetime.timedelta, and corresponding Pandas classes). This causes serialization errors when viewing job details in the dashboard.

Proposal

It would be helpful if rq-dashboard allowed users to configure or pass a custom json.JSONEncoder (or a callable) for this serialization step. For example:

  • Add a configuration option (e.g., RQ_DASHBOARD_JSON_ENCODER) that points to a JSONEncoder subclass or function.
  • Or, allow passing an encoder when initializing the dashboard app.

For example:

from datetime import datetime

class CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return super().default(obj)

# somewhere in config
RQ_DASHBOARD_JSON_ENCODER = CustomEncoder

And the desired change in web.py:

json.dumps(job.get_meta(), cls=current_app.config.get("RQ_DASHBOARD_JSON_ENCODER", json.JSONEncoder))

Would you be open to a PR adding this configurability?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions