pythonjsondjangomezzaninecartridge

Decimal is not JSON serializable


I have a calculation that calculates the tax when a user checks out using Cartridge as my shop framework.

tax = tax * Decimal(str(settings.SHOP_DEFAULT_TAX_RATE))

The calculation works properly.I then pass tax to set_tax(request, _("GST+PST"), tax)

When I do that, I get an error Decimal('12.0') is not JSON serializable.

When I convert it to a float (set_tax(request, _("GST+PST"), float(tax)) it works; but when I go through the checkout I get this error:

unsupported operand type(s) for +=: 'Decimal' and 'float'

In my settings:

I've seen solutions where I need to use a custom JSONEncoder but the error occurs inside Django itself.

Thanks.


Solution

  • This was fixed a couple weeks ago but has not yet been released:

    https://github.com/stephenmcd/cartridge/commit/628bd203f39a62d9de2613de7057e6742657111f

    Fortunately you can add the development version as a dependency right now.

    BTW decimal is the correct type to be using.