Message319436
| 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:21 | biloup | set | recipients:
+ biloup |
| 2018年06月13日 08:44:21 | biloup | set | messageid: <1528879461.56.0.947875510639.issue33850@psf.upfronthosting.co.za> |
| 2018年06月13日 08:44:21 | biloup | link | issue33850 messages |
| 2018年06月13日 08:44:21 | biloup | create |
|