Skip to content

Commit 80641da

Browse files
committed
Add back pretty argument to json_encode
1 parent ed9f834 commit 80641da

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

graphql_server/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,15 @@ def load_json_body(data):
185185
raise HttpQueryError(400, "POST body sent invalid JSON.")
186186

187187

188-
def json_encode(data):
189-
# type: (Union[Dict,List]) -> str
188+
def json_encode(data, pretty=False):
189+
# type: (Union[Dict,List],Optional[bool]) -> str
190190
"""Serialize the given data(a dictionary or a list) using JSON.
191191
192192
The given data (a dictionary or a list) will be serialized using JSON
193193
and returned as a string that will be nicely formatted if you set pretty=True.
194194
"""
195+
if pretty:
196+
return json_encode_pretty(data)
195197
return json.dumps(data, separators=(",", ":"))
196198

197199

tests/test_helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def test_json_encode_pretty():
2525
assert result == '{\n "query": "{test}"\n}'
2626

2727

28+
def test_json_encode_with_pretty_argument():
29+
result = json_encode({"query": "{test}"}, pretty=False)
30+
assert result == '{"query":"{test}"}'
31+
result = json_encode({"query": "{test}"}, pretty=True)
32+
assert result == '{\n "query": "{test}"\n}'
33+
34+
2835
def test_load_json_body_as_dict():
2936
result = load_json_body('{"query": "{test}"}')
3037
assert result == {"query": "{test}"}

0 commit comments

Comments
 (0)