connexion icon indicating copy to clipboard operation
connexion copied to clipboard

Add support for modifying Jsonifier dumps_args

Open advance512 opened this issue 5 years ago • 2 comments

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

advance512 avatar May 13 '20 18:05 advance512

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

tomskikh avatar Apr 30 '21 04:04 tomskikh

Yeah, I already have a workaround @tomskikh :) I was hoping for a more robust solution, as part of the project.

advance512 avatar Oct 18 '21 20:10 advance512

You can pass a jsonifier to your connexion App or API in Connexion 3, which will be released tomorrow.

RobbeSneyders avatar Nov 01 '23 17:11 RobbeSneyders