diff options
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) |