Will json.dump() get the indent argument? #16296
-
I am wondering if adding "indent" argument to json.dump() may be in any plan. My use case is to write json strings to the dev board so it can be later opened say in Thonny and easily modified by hand. Currently there is no indent option so everything is turned into a single line of string, impossible to read. If there is no plan to add this feature, is there any easy way to obtain this feature by other means? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
There have been a few PRs to try to reduce the size of the generated json by omitting spaces #4294 , #7337 Eve after that - there is a pitfall waiting to happen if you try to
This is very likely to cause filesystem corruption , see #16012 for one of the recent conversations on this topic. So my answer is : Quite unlikely If you know something about the json structure, you may be able to save memory , and add indentation at the same time. The gist: # write 'header'
# write json by node to reduce memory requirements
with open(self._json_name, "w") as f:
f.write("{")
f.write(dumps({"firmware": self.info})[1:-1])
f.write(",\n")
f.write(dumps({"stubber": {"version": __version__}, "stubtype": "firmware"})[1:-1])
f.write(",\n")
f.write('"modules" :[\n')
# Write data array
with open(self._json_name, "a") as f:
if not self._json_first:
f.write(",\n")
else:
self._json_first = False
# Add indentation to the line below
indent = 4
line = '{{{}"module": "{}", "file": "{}"}}'.format(" "*indent, module_name, stub_file.replace("\\", "/"))
f.write(line)
# Write 'trailer'
with open(self._json_name, "a") as f:
f.write("\n]}") |
Beta Was this translation helpful? Give feedback.
There have been a few PRs to try to reduce the size of the generated json by omitting spaces #4294 , #7337
adding
ident=4
would be inline with CPython , but I thing the code for that is non-trivial and will increase the firmware size.Eve after that - there is a pitfall waiting to happen if you try to
This is very likely to cause filesystem corruption , see #16012 for one of the recent conversations on this topic.
So my answer is : Quite unlikely
If you know something about the json structure, you may be able to save memory , and add indentation at the same time.
I have …