Skip to content

Commit 8ca7a74

Browse files
committed
use jsonify instead of default json lib for a better interaction with date and datetime
1 parent 5b90620 commit 8ca7a74

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

Diff for: flask_rest_jsonapi/decorators.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
"""Decorators to check headers and method requirements for each Api calls"""
44

5-
import json
65
from functools import wraps
76

8-
from flask import request, make_response
7+
from flask import request, make_response, jsonify
98

109
from flask_rest_jsonapi.errors import jsonapi_errors
1110

@@ -20,16 +19,16 @@ def check_headers(func):
2019
def wrapper(*args, **kwargs):
2120
if request.method in ('POST', 'PATCH'):
2221
if 'Content-Type' not in request.headers or request.headers['Content-Type'] != 'application/vnd.api+json':
23-
error = json.dumps(jsonapi_errors([{'source': '',
24-
'detail': "Content-Type header must be application/vnd.api+json",
25-
'title': 'InvalidRequestHeader',
26-
'status': 415}]))
22+
error = jsonify(jsonapi_errors([{'source': '',
23+
'detail': "Content-Type header must be application/vnd.api+json",
24+
'title': 'InvalidRequestHeader',
25+
'status': 415}]))
2726
return make_response(error, 415, {'Content-Type': 'application/vnd.api+json'})
2827
if request.headers.get('Accept') and 'application/vnd.api+json' not in request.accept_mimetypes:
29-
error = json.dumps(jsonapi_errors([{'source': '',
30-
'detail': "Accept header must be application/vnd.api+json",
31-
'title': 'InvalidRequestHeader',
32-
'status': 406}]))
28+
error = jsonify(jsonapi_errors([{'source': '',
29+
'detail': "Accept header must be application/vnd.api+json",
30+
'title': 'InvalidRequestHeader',
31+
'status': 406}]))
3332
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
3433
return func(*args, **kwargs)
3534
return wrapper

Diff for: flask_rest_jsonapi/resource.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"""This module contains the logic of resource management"""
44

55
import inspect
6-
import json
76
from six import with_metaclass
87

98
from werkzeug.wrappers import Response
10-
from flask import request, url_for, make_response, current_app
9+
from flask import request, url_for, make_response, current_app, jsonify
1110
from flask.views import MethodView, MethodViewType
1211
from marshmallow_jsonapi.exceptions import IncorrectTypeError
1312
from marshmallow import ValidationError
@@ -70,14 +69,14 @@ def dispatch_request(self, *args, **kwargs):
7069
try:
7170
response = method(*args, **kwargs)
7271
except JsonApiException as e:
73-
return make_response(json.dumps(jsonapi_errors([e.to_dict()])),
72+
return make_response(jsonify(jsonapi_errors([e.to_dict()])),
7473
e.status,
7574
headers)
7675
except Exception as e:
7776
if current_app.config['DEBUG'] is True:
7877
raise e
7978
exc = JsonApiException('', str(e))
80-
return make_response(json.dumps(jsonapi_errors([exc.to_dict()])),
79+
return make_response(jsonify(jsonapi_errors([exc.to_dict()])),
8180
exc.status,
8281
headers)
8382

@@ -88,7 +87,7 @@ def dispatch_request(self, *args, **kwargs):
8887
if not isinstance(response, tuple):
8988
if isinstance(response, dict):
9089
response.update({'jsonapi': {'version': '1.0'}})
91-
return make_response(json.dumps(response), 200, headers)
90+
return make_response(jsonify(response), 200, headers)
9291

9392
try:
9493
data, status_code, headers = response
@@ -104,7 +103,7 @@ def dispatch_request(self, *args, **kwargs):
104103
if isinstance(data, dict):
105104
data.update({'jsonapi': {'version': '1.0'}})
106105

107-
return make_response(json.dumps(data), status_code, headers)
106+
return make_response(jsonify(data), status_code, headers)
108107

109108

110109
class ResourceList(with_metaclass(ResourceMeta, Resource)):

0 commit comments

Comments
 (0)