utils: raspberrypi: ctt: json_pretty_print: Fix indentation handling

Indentation is handled by outputting spaces right after outputting a
newline character. That works in most cases, but would result in the
input '{}' being printed as

{
    }

instead of

{
}

Fix it by outputting the indentation before outputting the next
character after a newline. The indentation value will be updated by
then.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
Tested-by: David Plowman <david.plowman@raspberrypi.com>
This commit is contained in:
Laurent Pinchart 2020-07-03 01:43:57 +03:00
parent 606741deb9
commit d9617a499a

View file

@ -16,16 +16,20 @@ class JSONPrettyPrinter(object):
"indent": 0, "indent": 0,
"inarray": [False], "inarray": [False],
"arraycount": [], "arraycount": [],
"skipnewline": True "skipnewline": True,
"need_indent": False,
} }
self.fout = fout self.fout = fout
def newline(self): def newline(self):
self.fout.write('\n') self.fout.write('\n')
self.fout.write(' ' * self.state["indent"] * 4) self.state["need_indent"] = True
def write(self, c): def write(self, c):
if self.state["need_indent"]:
self.fout.write(' ' * self.state["indent"] * 4)
self.state["need_indent"] = False
self.fout.write(c) self.fout.write(c)
def process_char(self, c): def process_char(self, c):