diff options
Diffstat (limited to 'mitmproxy/contentviews/json.py')
-rw-r--r-- | mitmproxy/contentviews/json.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/mitmproxy/contentviews/json.py b/mitmproxy/contentviews/json.py new file mode 100644 index 00000000..de7f1093 --- /dev/null +++ b/mitmproxy/contentviews/json.py @@ -0,0 +1,27 @@ +import json +from typing import Optional + +from mitmproxy.contentviews import base + + +def pretty_json(s: bytes) -> Optional[bytes]: + try: + p = json.loads(s.decode('utf-8')) + except ValueError: + return None + pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False) + return pretty.encode("utf8", "strict") + + +class ViewJSON(base.View): + name = "JSON" + prompt = ("json", "s") + content_types = [ + "application/json", + "application/vnd.api+json" + ] + + def __call__(self, data, **metadata): + pj = pretty_json(data) + if pj: + return "JSON", base.format_text(pj) |