From 588a0e815c790b1a5da6da5d05f5543ea01885ac Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Fri, 3 Aug 2018 20:38:46 +0200 Subject: [PATCH] Debug json : now valid compact JSON (RFC 4627) --- circuitpython/code/main.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/circuitpython/code/main.py b/circuitpython/code/main.py index 986fc1f..fb6ef79 100644 --- a/circuitpython/code/main.py +++ b/circuitpython/code/main.py @@ -117,14 +117,27 @@ class Data: def json(self): """Serialize data to json-formatted string""" - output = "{" + output = '{' + first_source = True for source in self.data.keys(): - output = "".join((output, "'", source, "': \n")) + if first_source: + first_source = False + comma_src = "" + else: + comma_src = "," + output = '{}{}"{}":'.format(output, comma_src, source) if not self.data[source] == None: + output = output + '{' + first_data = True for d in self.data[source].items(): - output = "".join((output, " {'{}': '{}'}}\n".format(d[0], d[1]))) - output = output + "}, \n" - output = output + "}" + if first_data: + comma = "" + first_data = False + else: + comma = "," + output = '{}{}"{}":"{}"'.format(output, comma, d[0], d[1]) + output = output + '}' + output = output + '}' return output def atmo2rgb(self): @@ -279,7 +292,7 @@ while True: data.update() if print_data: data.show() - # print(data.json()) + print(data.json()) if backup_data: data.write_on_flash() if data_to_neopixel: