New submission from Clément Boyer <clem...@hacknowledge.ch>: I use a class to write easily json when having generator. ```python class StreamArray(list): def __init__(self, generator): super().__init__() self.generator = generator def __iter__(self): return self.generator def __len__(self): return 1 ``` Below a test comparing json.dump and json.dumps. ``` >>> import json >>> class StreamArray(list): ... def __init__(self, generator): ... super().__init__() ... self.generator = generator ... def __iter__(self): ... return self.generator ... def __len__(self): ... return 1 ... >>> g = (i for i in range(0)) >>> json.dumps({"a": StreamArray(g)}) '{"a": []}' >>> f = open("/tmp/test.json", "w+") >>> g = (i for i in range(0)) >>> json.dump({"a": StreamArray(g)}, f) >>> f.close() >>> print(open("/tmp/test.json").read()) {"a": ]} ``` I don't know if it's me or if there is actually a problem, could you help me ?
---------- components: Library (Lib) messages: 319436 nosy: biloup priority: normal severity: normal status: open title: Json.dump() bug when using generator type: behavior versions: Python 3.6 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue33850> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com