[issue20774] collections.deque should ship with a stdlib json serializer

Gareth Rees report at bugs.python.org
Wed Feb 26 17:43:49 CET 2014


Gareth Rees added the comment:

The JSON implementation uses these tests to determine how to serialize a Python object:

    isinstance(o, (list, tuple))
    isinstance(o, dict)

So any subclasses of list and tuple are serialized as a list, and any subclass of dict is serialized as an object. For example:

    >>> json.dumps(collections.defaultdict())
    '{}'
    >>> json.dumps(collections.OrderedDict())
    '{}'
    >>> json.dumps(collections.namedtuple('mytuple', ())())
    '[]'

When deserialized, you'll get back a plain dictionary or list, so there's no round-trip property here.

The tests could perhaps be changed to:

    isinstance(o, collections.abc.Sequence)
    isinstance(o, collections.abc.Mapping)

I'm not a JSON expert, so I have no informed opinion on whether this is a good idea or not, but in any case, this change wouldn't help with deques, as a deque is not a Sequence. That's because deques don't have an index method (see issue10059 and issue12543).

----------
nosy: +Gareth.Rees

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20774>
_______________________________________


More information about the Python-bugs-list mailing list