[New-bugs-announce] [issue16057] Subclasses of JSONEncoder should not be insturcted to call JSONEncoder.decode

Justin Lebar report at bugs.python.org
Wed Sep 26 23:40:43 CEST 2012


New submission from Justin Lebar:

The JSONEncoder documentation says we can implement our own encoder as:

  >>> class ComplexEncoder(json.JSONEncoder):
  ...     def default(self, obj):
  ...         if isinstance(obj, complex):
  ...             return [obj.real, obj.imag]
  ...         return json.JSONEncoder.default(self, obj)

Later on, we give the following example of how to implement the default method in a subclass of json.JSONEncoder:

  def default(self, o):
  try:
      iterable = iter(o)
  except TypeError:
      pass
  else:
      return list(iterable)
  return JSONEncoder.default(self, o)

These are both incorrect, as a quick reading of the source will reveal.  JSONEncoder.default() throws for all input values.  We should s/JSONEncoder.default/JSONEncoder.encode/ here, I think.

----------
assignee: docs at python
components: Documentation
messages: 171363
nosy: Justin.Lebar, docs at python
priority: normal
severity: normal
status: open
title: Subclasses of JSONEncoder should not be insturcted to call JSONEncoder.decode
versions: Python 2.7

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


More information about the New-bugs-announce mailing list