JSON Encoding#

It is often useful to be able to control how objects are encoded to and decoded from JSON. Quart makes this possible via the json_encoder and json_decoder to a custom JSONEncoder and JSONDecoder. This can be done for all routes via the Quart or for blueprint specific routes via json_encoder and json_decoder.

Money example#

As an example lets consider a Money object,

class Money:

    def __init__(self, amount: Decimal, currency: str) -> None:
        self.amount = amount
        self.currency = currency

which we desire to translate to JSON as,

  "amount": "10.00",
  "currency": "GBP"

using encoders and decoders as so,

class MoneyJSONEncoder(json.JSONEncoder):

    def default(self, object_):
        if isinstance(object_, Money):
            return {'amount': object_.amount, 'currency': object_.currency}
            return super().default(object_)

class MoneyJSONDecoder(json.JSONDecoder):

    def __init__(self, *args, **kwargs):
        super().__init__(object_hook=self.dict_to_object, *args, **kwargs)

    def dict_to_object(self, dict_):
        if 'amount' in dict_ and 'currency' in dict_:
            return Money(Decimal(dict_['amount']), dict_['currency'])
            return dict_

app.json_decoder = MoneyJSONDecoder
app.json_encoder = MoneyJSONEncoder