mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 10:16:27 +00:00
colorize json
This commit is contained in:
parent
ce50e8e52d
commit
454f1779f0
@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
import json
|
import json
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@ -9,8 +10,7 @@ def pretty_json(s: bytes) -> Optional[bytes]:
|
|||||||
p = json.loads(s.decode('utf-8'))
|
p = json.loads(s.decode('utf-8'))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False)
|
return p
|
||||||
return pretty.encode("utf8", "strict")
|
|
||||||
|
|
||||||
|
|
||||||
class ViewJSON(base.View):
|
class ViewJSON(base.View):
|
||||||
@ -21,7 +21,30 @@ class ViewJSON(base.View):
|
|||||||
"application/vnd.api+json"
|
"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):
|
def __call__(self, data, **metadata):
|
||||||
pj = pretty_json(data)
|
pj = pretty_json(data)
|
||||||
if pj:
|
if pj is not None:
|
||||||
return "JSON", base.format_text(pj)
|
return "JSON", self._format(pj)
|
||||||
|
@ -35,6 +35,9 @@ class Palette:
|
|||||||
# Hex view
|
# Hex view
|
||||||
'offset',
|
'offset',
|
||||||
|
|
||||||
|
# JSON view
|
||||||
|
'json_string', 'json_number', 'json_boolean',
|
||||||
|
|
||||||
# Grid Editor
|
# Grid Editor
|
||||||
'focusfield', 'focusfield_error', 'field_error', 'editfield',
|
'focusfield', 'focusfield_error', 'field_error', 'editfield',
|
||||||
|
|
||||||
@ -170,6 +173,11 @@ class LowDark(Palette):
|
|||||||
# Hex view
|
# Hex view
|
||||||
offset = ('dark cyan', 'default'),
|
offset = ('dark cyan', 'default'),
|
||||||
|
|
||||||
|
# JSON view
|
||||||
|
json_string = ('dark blue', 'default'),
|
||||||
|
json_number = ('light magenta', 'default'),
|
||||||
|
json_boolean = ('dark magenta', 'default'),
|
||||||
|
|
||||||
# Grid Editor
|
# Grid Editor
|
||||||
focusfield = ('black', 'light gray'),
|
focusfield = ('black', 'light gray'),
|
||||||
focusfield_error = ('dark red', 'light gray'),
|
focusfield_error = ('dark red', 'light gray'),
|
||||||
@ -270,6 +278,11 @@ class LowLight(Palette):
|
|||||||
# Hex view
|
# Hex view
|
||||||
offset = ('dark blue', 'default'),
|
offset = ('dark blue', 'default'),
|
||||||
|
|
||||||
|
# JSON view
|
||||||
|
json_string = ('dark blue', 'default'),
|
||||||
|
json_number = ('light magenta', 'default'),
|
||||||
|
json_boolean = ('dark magenta', 'default'),
|
||||||
|
|
||||||
# Grid Editor
|
# Grid Editor
|
||||||
focusfield = ('black', 'light gray'),
|
focusfield = ('black', 'light gray'),
|
||||||
focusfield_error = ('dark red', 'light gray'),
|
focusfield_error = ('dark red', 'light gray'),
|
||||||
@ -380,6 +393,11 @@ class SolarizedLight(LowLight):
|
|||||||
# Hex view
|
# Hex view
|
||||||
offset = (sol_cyan, 'default'),
|
offset = (sol_cyan, 'default'),
|
||||||
|
|
||||||
|
# JSON view
|
||||||
|
json_string = (sol_cyan, 'default'),
|
||||||
|
json_number = (sol_blue, 'default'),
|
||||||
|
json_boolean = (sol_magenta, 'default'),
|
||||||
|
|
||||||
# Grid Editor
|
# Grid Editor
|
||||||
focusfield = (sol_base00, sol_base2),
|
focusfield = (sol_base00, sol_base2),
|
||||||
focusfield_error = (sol_red, sol_base2),
|
focusfield_error = (sol_red, sol_base2),
|
||||||
@ -454,6 +472,11 @@ class SolarizedDark(LowDark):
|
|||||||
# Hex view
|
# Hex view
|
||||||
offset = (sol_cyan, 'default'),
|
offset = (sol_cyan, 'default'),
|
||||||
|
|
||||||
|
# JSON view
|
||||||
|
json_string = (sol_cyan, 'default'),
|
||||||
|
json_number = (sol_blue, 'default'),
|
||||||
|
json_boolean = (sol_magenta, 'default'),
|
||||||
|
|
||||||
# Grid Editor
|
# Grid Editor
|
||||||
focusfield = (sol_base0, sol_base02),
|
focusfield = (sol_base0, sol_base02),
|
||||||
focusfield_error = (sol_red, sol_base02),
|
focusfield_error = (sol_red, sol_base02),
|
||||||
|
@ -14,3 +14,5 @@ def test_view_json():
|
|||||||
assert v(b"{}")
|
assert v(b"{}")
|
||||||
assert not v(b"{")
|
assert not v(b"{")
|
||||||
assert v(b"[1, 2, 3, 4, 5]")
|
assert v(b"[1, 2, 3, 4, 5]")
|
||||||
|
assert v(b'{"foo" : 3}')
|
||||||
|
assert v(b'{"foo": true, "nullvalue": null}')
|
||||||
|
Loading…
Reference in New Issue
Block a user