Skip to content

Commit

Permalink
Support Decimal type in JSON serializer
Browse files Browse the repository at this point in the history
SQLa Numeric types get returned as decimal.Decimal
objects. simplejson has support for this, whereas
the builtin json module does not and there's no clean
way to subclass it in a way that it won't convert
the value to a JSON string.

Fixes ColtonProvias#23
  • Loading branch information
Greg Hill committed Aug 20, 2016
1 parent 9b8a8db commit 45843ee
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ target/
pyvenv.cfg
pip-selfcheck.json

# PyCharm
.idea

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ passlib==1.6.5
pathtools==0.1.2
SQLAlchemy-Utils==0.30.16
pytest==2.7.3
simplejson==3.8.2
5 changes: 3 additions & 2 deletions sqlalchemy_jsonapi/flaskext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import datetime
import json
import simplejson as json
import uuid
from functools import wraps

Expand Down Expand Up @@ -35,6 +35,7 @@ def default(self, value):
return str(value)
return json.JSONEncoder.default(self, value)


#: The views to generate
views = [
(Method.GET, Endpoint.COLLECTION), (Method.GET, Endpoint.RESOURCE),
Expand Down Expand Up @@ -235,7 +236,7 @@ def new_view(**kwargs):
response = override(exc, results)
rendered_response = make_response('')
if response.status_code != 204:
data = json.dumps(response.data, cls=self.json_encoder)
data = json.dumps(response.data, cls=self.json_encoder, use_decimal=True)
rendered_response = make_response(data)
rendered_response.status_code = response.status_code
rendered_response.content_type = 'application/vnd.api+json'
Expand Down

0 comments on commit 45843ee

Please sign in to comment.