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
|
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2019, Raspberry Pi (Trading) Limited
#
# ctt_pretty_print_json.py - camera tuning tool JSON formatter
"""
takes a collapsed json file and makes it more readable
"""
def process_file(string, fout, state):
for c in string:
process_char(c, fout, state)
def print_newline(fout, state):
fout.write('\n')
fout.write(' '*state["indent"]*4)
def process_char(c, fout, state):
if c == '{':
if not state["skipnewline"]: print_newline(fout, state)
fout.write(c)
state["indent"] += 1
print_newline(fout, state)
elif c == '}':
state["indent"] -= 1
print_newline(fout, state)
fout.write(c)
elif c == '[':
print_newline(fout, state)
fout.write(c)
state["indent"] += 1
print_newline(fout, state)
state["inarray"] = [True] + state["inarray"]
state["arraycount"] = [0] + state["arraycount"]
elif c == ']':
state["indent"] -= 1
print_newline(fout, state)
state["inarray"].pop(0)
state["arraycount"].pop(0)
fout.write(c)
elif c == ':':
fout.write(c)
fout.write(' ')
elif c == ' ':
pass
elif c == ',':
if not state["inarray"][0]:
fout.write(c)
fout.write(' ')
print_newline(fout, state)
else:
fout.write(c)
state["arraycount"][0] += 1
if state["arraycount"][0] == 16:
state["arraycount"][0] = 0
print_newline(fout, state)
else:
fout.write(' ')
else:
fout.write(c)
state["skipnewline"] = (c == '[')
def pretty_print_json(str_in, output_filename):
state = {"indent": 0, "inarray": [False], "arraycount": [], "skipnewline": True}
with open(output_filename, "w") as fout:
process_file(str_in, fout, state)
if __name__ == '__main__':
pretty_print_json("../ctt/ref_json/final_imx477.json", "pretty.json")
|