1
0
mirror of https://github.com/httpie/cli.git synced 2026-06-20 11:32:56 +02:00
Files
httpie-cli/httpie/output/formatters/json.py
T

27 lines
722 B
Python
Raw Normal View History

2014-04-27 00:07:13 +02:00
from __future__ import absolute_import
import json
from httpie.plugins import FormatterPlugin
2014-04-27 00:07:13 +02:00
DEFAULT_INDENT = 4
2014-04-27 00:07:13 +02:00
class JSONFormatter(FormatterPlugin):
def format_body(self, body, mime):
2014-04-27 00:07:13 +02:00
if 'json' in mime:
try:
obj = json.loads(body)
except ValueError:
# Invalid JSON, ignore.
pass
else:
# Indent, sort keys by name, and avoid
# unicode escapes to improve readability.
body = json.dumps(obj,
sort_keys=True,
ensure_ascii=False,
indent=DEFAULT_INDENT)
return body