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.

classification
Title: json custom encoder not fully functionnal
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: bob.ippolito Nosy List: bob.ippolito, phanser
Priority: normal Keywords:

Created on 2009-03-22 08:03 by phanser, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
jsonCustomEncoder.py phanser, 2009-03-22 08:03 minimum example which manifests the problem
Messages (4)
msg83962 - (view) Author: Pierre Hanser (phanser) Date: 2009-03-22 08:03
The json module provides an encoder python -> json.
The encoding may be specialized by the user, using
the cls parameter of the dumps function.

But all simple types are always handled by the library encoder,
the user encoder is only used as a last resort one, for unknown types.

This is not described nor intuitive, and it prevents specifying a custom
encoder for classes which inherit from simple type.

in the provided example (thanks Raymond) a user defined boolean
type, inheriting from int, is handled as int where you would prefer
it to be handled as a json boolean through a custom encoder.

problem seen on simplejson-2.0.9 or official python 2.6.1
msg83972 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2009-03-22 16:17
It is common to specify a default function but it would be terrible for 
performance if this function was called for every single object passed 
through to the decoder.

If you want a serialization different from a primitive type you'll have to  
choose a different base class.
msg83980 - (view) Author: Pierre Hanser (phanser) Date: 2009-03-22 19:49
you realize that the current handling is incorrect and also not documented?

and that the described case is from the real world: it prevents using pydbus
and dbus.Boolean type to send a json boolean

I don't take your argument about performance:
i would like to have the choice between correctness and performance
msg83981 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2009-03-22 20:15
The documentation says "If specified, default is a function that gets 
called for objects that can’t otherwise be serialized. It should return a 
JSON encodable version of the object or raise a TypeError."

*can't otherwise be serialized* means that the object must not be a 
subtype of something that is serializable. The implementation is correct 
and this behavior is documented. Documentation patches to make this more 
obvious are acceptable, but it's not a bug so it can't be fixed as such.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49785
2009-03-22 20:15:33bob.ippolitosetmessages: + msg83981
2009-03-22 19:49:07phansersetmessages: + msg83980
2009-03-22 16:18:31benjamin.petersonsetstatus: open -> closed
2009-03-22 16:17:35bob.ippolitosetresolution: wont fix
messages: + msg83972
2009-03-22 13:46:33georg.brandlsetassignee: bob.ippolito

nosy: + bob.ippolito
2009-03-22 08:03:39phansercreate