mirror of
https://github.com/httpie/cli.git
synced 2024-11-25 09:13:25 +01:00
a3a08a9a22
* Use relative imports in test * Use relative imports * Add myself to contributors :)
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
from __future__ import absolute_import
|
|
import json
|
|
|
|
from ...plugins import FormatterPlugin
|
|
|
|
|
|
class JSONFormatter(FormatterPlugin):
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.enabled = self.format_options['json']['format']
|
|
|
|
def format_body(self, body: str, mime: str) -> str:
|
|
maybe_json = [
|
|
'json',
|
|
'javascript',
|
|
'text',
|
|
]
|
|
if (self.kwargs['explicit_json']
|
|
or any(token in mime for token in maybe_json)):
|
|
try:
|
|
obj = json.loads(body)
|
|
except ValueError:
|
|
pass # Invalid JSON, ignore.
|
|
else:
|
|
# Indent, sort keys by name, and avoid
|
|
# unicode escapes to improve readability.
|
|
body = json.dumps(
|
|
obj=obj,
|
|
sort_keys=self.format_options['json']['sort_keys'],
|
|
ensure_ascii=False,
|
|
indent=self.format_options['json']['indent']
|
|
)
|
|
return body
|