Last active
January 8, 2026 04:36
-
-
Save tsuchm/bcb79db7fe9750eb34e3dbb872718fae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from itertools import pairwise | |
| def sentinel(iterable): | |
| for x in iterable: | |
| yield x | |
| yield None | |
| def write_json_streaming_style(generator, indent=4): | |
| print("[") | |
| for cur,post in pairwise(sentinel(generator)): | |
| text = json.dumps(cur, ensure_ascii=False, indent=indent) | |
| if post: | |
| text += "," | |
| for line in text.split("\n"): | |
| print(" " * indent + line) | |
| print("]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment