falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.51k stars 937 forks source link

How to change option for json parser ? #1906

Closed Stargateur closed 3 years ago

Stargateur commented 3 years ago

I read https://falcon.readthedocs.io/en/stable/api/media.html but I fail to understand how I could simply update option for the json parser, or just replace it.

open-collective-bot[bot] commented 3 years ago

Hi :wave:,

Thanks for using Falcon. The large amount of time and effort needed to maintain the project and develop new features is not sustainable without the generous financial support of community members like you.

Please consider helping us secure the future of the Falcon framework with a one-time or recurring donation.

Thank you for your support!

vytas7 commented 3 years ago

Hi @Stargateur!

You need to customize the JSON media handler. Under JSONHandler, there are a couple of snippets how to use a different JSON library, or how to pass custom parameters to it.

Once you have a customized handler, you need to change the application's request and/or response options to use your custom handler object for application/json (or any other Internet media type). From one of those snippets:

extra_handlers = {
    'application/json': json_handler,
}

app = falcon.App()
app.req_options.media_handlers.update(extra_handlers)
app.resp_options.media_handlers.update(extra_handlers)

See also: Replacing the Default Handlers.

Does any of the above shed some light on the subject? Or did you mean some other part of the framework by "the JSON parser"?

Stargateur commented 3 years ago

Yes, I have read the page, but I don't understand how to use this to change an option, put it simply I would like that the default use str.

I have understand that I need to use https://docs.python.org/fr/3/library/json.html#json.dumps but I'm not a python dev so it's hard for me to guess what to do next. Or maybe use https://docs.python.org/fr/3/library/json.html#json.JSONEncoder but here again I don't have much clue about what to do.

And... I think I understand by writing this:

from functools import partial

from falcon import media
import json

app = falcon.App()
json_handler = media.JSONHandler(
    dumps=partial(
        json.dumps,
        ensure_ascii=False,
        default=str,
    ),
)
extra_handlers = {
    'application/json': json_handler,
}
app.resp_options.media_handlers.update(extra_handlers)

It's confusing example say the default use json from std but use rapidjson in example. But this seem to work perfectly thanks.

vytas7 commented 3 years ago

Ah, right, that's a fair point; maybe we're conflating those two concepts (using a custom JSON library vs just customizing the stdlib's json) in a way that it gets confusing. It does make sense to clearly illustrate both concepts separately.

We also have this recipe excercising customization of the standard json.dumps: Prettifying JSON Responses, although it seems you got it under control now nevertheless.