diff options
author | root <skkaushik212@gmail.com> | 2020-04-12 17:13:34 +0530 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2020-04-19 12:49:09 +0200 |
commit | 454f1779f098396b46bc8fe53a9820926a284cd4 (patch) | |
tree | b176d5a5203ac99c534071654d1af3a2dd649028 /mitmproxy/contentviews/json.py | |
parent | ce50e8e52dc5316f9be29bc00d0dc72fc2b0af83 (diff) | |
download | mitmproxy-454f1779f098396b46bc8fe53a9820926a284cd4.tar.gz mitmproxy-454f1779f098396b46bc8fe53a9820926a284cd4.tar.bz2 mitmproxy-454f1779f098396b46bc8fe53a9820926a284cd4.zip |
colorize json
Diffstat (limited to 'mitmproxy/contentviews/json.py')
-rw-r--r-- | mitmproxy/contentviews/json.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/mitmproxy/contentviews/json.py b/mitmproxy/contentviews/json.py index 15c624ad..dbe600aa 100644 --- a/mitmproxy/contentviews/json.py +++ b/mitmproxy/contentviews/json.py @@ -1,3 +1,4 @@ +import re import json from typing import Optional @@ -9,8 +10,7 @@ def pretty_json(s: bytes) -> Optional[bytes]: 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") + return p class ViewJSON(base.View): @@ -21,7 +21,30 @@ class ViewJSON(base.View): "application/vnd.api+json" ] + @staticmethod + def _format(pj): + li = [] + for chunk in json.JSONEncoder(indent=4, sort_keys=True, ensure_ascii=False).iterencode(pj): + k = re.split("\\n", chunk) + if(len(k) > 1): + if(len(k[0]) > 0): + li.append(('text', k[0])) + yield li + li = [] + chunk = k[1] + else: + chunk = k[0] + if(re.match('^\s*\".*\"$', chunk)): + li.append(('json_string', chunk)) + elif(re.match('\s*[0-9]+[.]{0,1}[0-9]*', chunk)): + li.append(('json_number', chunk)) + elif(re.match('\s*true|null|false', chunk)): + li.append(('json_boolean', chunk)) + else: + li.append(('text', chunk)) + yield li + def __call__(self, data, **metadata): pj = pretty_json(data) - if pj: - return "JSON", base.format_text(pj) + if pj is not None: + return "JSON", self._format(pj) |