-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjj
57 lines (45 loc) · 1.11 KB
/
jj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import sys
import json
import demjson3
import ast
import logging
import argparse
from collections.abc import Iterable
from pygments import highlight, lexers, formatters
args = sys.argv[1:]
text = ''
if not args:
for line in sys.stdin.readlines():
text += line
else:
text = args[0]
if 0:
print(repr(text))
errors = []
dct = None
try: # JSON
dct = json.loads(text)
except Exception as e:
errors.append('JSON dict error: '+ str(e))
if dct is None:
try: # Python dict
dct = ast.literal_eval(text)
except Exception as e:
errors.append('Python dict error: '+str(e))
if dct is None:
try: # JavaScript Object
dct = demjson3.decode(text)
except Exception as e:
errors.append('JavaScript dict error: ' + str(e))
if dct is None:
for e in errors:
print(e)
sys.exit(1)
try:
formatted_json = json.dumps(dct, indent=4)
except Exception as e:
print('JSON dump error:', e)
sys.exit(1)
colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
print(colorful_json)