homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author biloup
Recipients biloup
Date 2018年06月13日.08:44:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1528879461.56.0.947875510639.issue33850@psf.upfronthosting.co.za>
In-reply-to
Content
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 ?
History
Date User Action Args
2018年06月13日 08:44:21biloupsetrecipients: + biloup
2018年06月13日 08:44:21biloupsetmessageid: <1528879461.56.0.947875510639.issue33850@psf.upfronthosting.co.za>
2018年06月13日 08:44:21bilouplinkissue33850 messages
2018年06月13日 08:44:21biloupcreate

AltStyle によって変換されたページ (->オリジナル) /