Skip to content

Commit

Permalink
Support Decimal values in JSONEncoder (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohabusama authored and hjacobs committed Mar 8, 2017
1 parent c936536 commit e9db7fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions connexion/decorators/produces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import functools
import logging

from decimal import Decimal

import flask
import six
from flask import json
Expand Down Expand Up @@ -30,6 +32,9 @@ def default(self, o):
if isinstance(o, datetime.date):
return o.isoformat()

if isinstance(o, Decimal):
return float(o)

return json.JSONEncoder.default(self, o)


Expand Down
9 changes: 9 additions & 0 deletions tests/test_produces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime
import json
import math

from decimal import Decimal

from connexion.decorators.produces import JSONEncoder

Expand All @@ -14,6 +17,12 @@ def test_json_encoder():
s = json.dumps(datetime.datetime.utcnow(), cls=JSONEncoder)
assert s.endswith('Z"')

s = json.dumps(Decimal(1.01), cls=JSONEncoder)
assert s == '1.01'

s = json.dumps(math.expm1(1e-10), cls=JSONEncoder)
assert s == '1.00000000005e-10'


def test_json_encoder_datetime_with_timezone():

Expand Down

0 comments on commit e9db7fe

Please sign in to comment.