Add support for modifying Jsonifier dumps_args
Description
The default arguments of the standard Connexion Jsonifier class are (indent=2), these are then sent as kwargs to json.dumps().
class FlaskApi(AbstractAPI):
....
@classmethod
def _set_jsonifier(cls):
"""
Use Flask specific JSON loader
"""
cls.jsonifier = Jsonifier(flask.json, indent=2)
I'd like to configure these to be (indent=None, separators=(',', ':')), which is the most compact representation of JSON that the library can create.
The JSON outputted with the default configuration in an example API call is 56% longer compared to the most compact representation of the same JSON (using the second configuration).
Expected behaviour
Be able to configure the Jsonifier's default arguments, or supply a way to override _set_jsonifier().
Actual behaviour
Cannot configure the Jsonifier's default arguments. I was able to do the following monkeypatch to get the compact JSON representation:
connexionApp.api_cls.jsonifier.dumps_args = dict(indent=None, separators=(',', ':'))
Additional info:
- Python 3.8.0
- Connexion v2.6.0
You can use something like this as a workaround
class CompactJsonFlaskApi(FlaskApi):
@classmethod
def _set_jsonifier(cls):
cls.jsonifier = Jsonifier(flask.json, separators=(',', ':'))
app = connexion.App(__name__)
app.api_cls = CompactJsonFlaskApi
Yeah, I already have a workaround @tomskikh :) I was hoping for a more robust solution, as part of the project.
You can pass a jsonifier to your connexion App or API in Connexion 3, which will be released tomorrow.