mirror of
https://github.com/httpie/cli.git
synced 2025-01-10 07:38:35 +01:00
e4c68063b9
Still work in progress and the API should be considered private for now.
27 lines
722 B
Python
27 lines
722 B
Python
from __future__ import absolute_import
|
|
import json
|
|
|
|
from httpie.plugins import FormatterPlugin
|
|
|
|
|
|
DEFAULT_INDENT = 4
|
|
|
|
|
|
class JSONFormatter(FormatterPlugin):
|
|
|
|
def format_body(self, body, mime):
|
|
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
|