Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use flask machinery for json #631

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ancieg
Copy link

@ancieg ancieg commented Dec 26, 2024

flask-restx lacks of ability to switch JSON [de]serializer.
I prepared small patch to use flask machinery for JSON.

Usage example:

import logging
from datetime import datetime

import sdjson

from flask import Flask, current_app
from flask.json.provider import JSONProvider
from flask_restx import Api, Resource

logging.basicConfig(level=logging.INFO)


class SDJSONProvider(JSONProvider):
    def dumps(self, obj, **kwargs):
        current_app.logger.info("SDJSONProvider.dumps()")
        return sdjson.dumps(obj, default=str)

    def loads(self, s, **kwargs):
        current_app.logger.info("SDJSONProvider.loads()")
        return sdjson.loads(s, **kwargs)


class MyApp(Flask):
    json_provider_class = SDJSONProvider


app = MyApp(__name__)
api = Api(app)


@api.route("/date")
class DateResource(Resource):
    def get(self):
        return {"current_date": datetime.now()}


if __name__ == "__main__":
    app.run(debug=True)

another usage example:

import logging
from datetime import datetime

import sdjson

from flask import Flask, current_app
from flask.json.provider import JSONProvider
from flask_restx import Api, Resource, fields

logging.basicConfig(level=logging.INFO)


class SDJSONProvider(JSONProvider):
    def dumps(self, obj, **kwargs):
        current_app.logger.info("SDJSONProvider.dumps()")
        return sdjson.dumps(obj, default=SDJSONProvider._default)

    def loads(self, s, **kwargs):
        current_app.logger.info("SDJSONProvider.loads()")
        return sdjson.loads(s, **kwargs)

    @staticmethod
    def _default(obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return super().default(obj)


app = Flask(__name__)
app.json = SDJSONProvider(app)
api = Api(app)

hello_model = api.model(
    "Hello", {"name": fields.String(required=True, description="Your name")}
)


@api.route("/hello")
@api.expect(hello_model)
class HelloResource(Resource):
    def post(self):
        current_app.logger.info(api.payload)
        name = api.payload["name"]
        return (
            {
                "response": f"Hello, {name}!",
                "current_date": datetime.now(),
            },
            201,
        )


if __name__ == "__main__":
    app.run(debug=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant