Skip to content

Instantly share code, notes, and snippets.

@MrJake222
Created September 17, 2021 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrJake222/dd62bf958816690aa4ebdcd49b7909fb to your computer and use it in GitHub Desktop.
Save MrJake222/dd62bf958816690aa4ebdcd49b7909fb to your computer and use it in GitHub Desktop.
Alloc-less version of json.dump(s) for MicroPython.
async def json_dump_stream(obj, write):
if obj is None:
await write("null")
elif isinstance(obj, str):
await write('"')
await write(obj)
await write('"')
elif isinstance(obj, bool):
if obj:
await write('true')
else:
await write('false')
elif isinstance(obj, int) or isinstance(obj, float):
await write(str(obj))
elif isinstance(obj, bytes):
# treating bytes as ascii string
await write('"')
for x in obj: await write(chr(x))
await write('"')
elif isinstance(obj, dict):
await write('{')
for i, key in enumerate(obj):
await write('"')
await write(key)
await write('":')
await json_dump_stream(obj[key], write)
if i < len(obj)-1: await write(',')
await write('}')
elif isinstance(obj, list) or isinstance(obj, tuple):
await write('[')
for i, entry in enumerate(obj):
await json_dump_stream(entry, write)
if i < len(obj)-1: await write(',')
await write(']')
else:
raise ValueError("unsupported value {}".format(type(obj)))
gc.collect()
f1 = gc.mem_free()
await json_dump_stream(self._body, stream_write)
gc.collect()
f2 = gc.mem_free()
logger.debug("took {} bytes of memory".format(f1-f2))
# outputs: took 0 bytes of memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment