Message161474
#!/usr/bin/python2.7
import json
class pseudo_list(object):
__class__ = list # fake isinstance
def __init__(self, iterator):
self._saved_iterator = iterator
def __iter__(self):
return self._saved_iterator
class myenc(json.JSONEncoder):
def default(self, o):
try:
return pseudo_list(iter(o))
except TypeError:
return super(myenc, self).default(o)
# works (pure-python implementation)
print json.dumps({1:xrange(10), 2:[5,6,7,8]}, cls=myenc, indent=1)
# does not work (C implementation)
print json.dumps({1:xrange(10), 2:[5,6,7,8]}, cls=myenc, indent=None) |
|
Date |
User |
Action |
Args |
2012-05-24 02:29:10 | socketpair | set | recipients:
+ socketpair, pitrou, cvrebert |
2012-05-24 02:29:10 | socketpair | set | messageid: <1337826550.48.0.16302893547.issue14886@psf.upfronthosting.co.za> |
2012-05-24 02:29:09 | socketpair | link | issue14886 messages |
2012-05-24 02:29:09 | socketpair | create | |
|