This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_formatter.py
90 lines (79 loc) · 2.54 KB
/
json_formatter.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- encoding: utf-8 -*-
class JsonFormatter:
def __init__(self, intend=4, name="", encoding="utf-8"):
self.name = name
self.intend = intend
self.encoding = encoding
self.stack = []
self.obj = None
self.source = self.get_source(name, self.encoding)
self.prepare()
@staticmethod
def json_str(s):
return '"' + s + '"'
@staticmethod
def get_source(name, encoding="utf-8"):
with open(name, 'r', encoding=encoding) as f:
return ''.join(f.read().split())
def prepare(self):
try:
self.source = self.source.replace("null", "None").replace("true", "True").replace("false", "False")
self.obj = eval(self.source)
except:
raise Exception('Invalid json string!')
def line_intend(self, level=0):
return '\n' + ' ' * self.intend * level
def parse_dict(self,obj=None,intend_level=0):
if intend_level == 0:
# 这个判断是为了防止文件开头出现空行
self.stack.append('{')
else:
self.stack.append(self.line_intend(intend_level)+'{')
intend_level += 1
i = 0
for key, value in obj.items():
key = self.json_str(str(key))
self.stack.append(self.line_intend(intend_level)+key+':')
self.parse(value, intend_level)
if i != len(obj.items())-1:
# 这个处理是为了防止最后一对kv后面还有个逗号,这样会造成json.load()函数无法读取
self.stack.append(',')
i += 1
self.stack.append(self.line_intend(intend_level-1)+'}')
def parse_list(self, obj=None, intend_level=0):
if intend_level == 0:
self.stack.append('[')
else:
self.stack.append(self.line_intend(intend_level)+'[')
intend_level += 1
for i, item in zip(range(0, len(obj)), obj):
self.parse(item, intend_level)
if i != len(obj)-1:
self.stack.append(',')
self.stack.append(self.line_intend(intend_level-1)+']')
def parse(self, obj, intend_level=0):
if obj is None:
self.stack.append('null')
elif obj is True:
self.stack.append('true')
elif obj is False:
self.stack.append('false')
elif isinstance(obj, (int, float)):
self.stack.append(str(obj))
elif isinstance(obj, str):
self.stack.append(self.json_str(obj))
elif isinstance(obj, (list, tuple)):
self.parse_list(obj, intend_level)
elif isinstance(obj, dict):
self.parse_dict(obj, intend_level)
else:
raise Exception('Invalid json type %s!' % obj)
def render(self):
self.parse(self.obj, 0)
res_file = self.name
res = ''.join(self.stack)
with open(res_file, 'w', encoding=self.encoding) as f:
f.write(res)
if __name__ == "__main__":
jf = JsonFormatter(name="/Users/Mac/Desktop/w2o/tst/tst.json")
jf.render()