You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
768 B
27 lines
768 B
2 years ago
|
try:
|
||
|
from ujson import dumps
|
||
|
except ImportError:
|
||
|
from json import dumps
|
||
|
|
||
5 years ago
|
from flask import make_response, current_app
|
||
|
|
||
|
|
||
|
def output_json(data, code, headers=None):
|
||
|
"""Makes a Flask response with a JSON encoded body"""
|
||
|
|
||
2 years ago
|
settings = current_app.config.get("RESTX_JSON", {})
|
||
5 years ago
|
|
||
|
# If we're in debug mode, and the indent is not set, we set it to a
|
||
|
# reasonable value here. Note that this won't override any existing value
|
||
2 years ago
|
# that was set.
|
||
5 years ago
|
if current_app.debug:
|
||
2 years ago
|
settings.setdefault("indent", 4)
|
||
5 years ago
|
|
||
|
# always end the json dumps with a new line
|
||
|
# see https://github.com/mitsuhiko/flask/pull/1262
|
||
|
dumped = dumps(data, **settings) + "\n"
|
||
|
|
||
|
resp = make_response(dumped, code)
|
||
|
resp.headers.extend(headers or {})
|
||
|
return resp
|