Author serhiy.storchaka
Recipients Daniel Ward, Ollie Ford, abarry, berker.peksag, bob.ippolito, r.david.murray, rhettinger, serhiy.storchaka, steven.daprano
Date 2017-03-05.10:03:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1488708239.15.0.773031076559.issue27362@psf.upfronthosting.co.za>
In-reply-to
Content
This feature already was proposed for simplejson (https://github.com/simplejson/simplejson/issues/52). Special __json__ method is used in wild in a number of projects for exactly this purpose. It looks to me the main disagreement in the past Python-Idea discussion (https://mail.python.org/pipermail/python-ideas/2010-July/007811.html) was about whether implement the customization as a special method or as a registry. I suggest to implement both. Special methods are good for standard collection and numeric classes, global registry is good for application-wide serialization, local dispatch table or the default method are good for more specific task-specific customization.

Here is a draft implementation. It follows the design of pickle and copy modules.

There are few design questions.

1. What is the order of using different customization methods? Should registries and __json__ be checked before calling the default method, after calling the default method (if it fails), or inside the default implementation of the default method?

2. For Decimal we need to customize raw JSON representation. In the past it was possible to implement an intermediate float or int subclass with __str__ or __repr__ returning raw JSON representation. But this hack no longer works. Needed to add explicit support of special JSON representation objects. Other way -- add yet one special method (__raw_json__ or __json_str__).

3. Do we need the json.registry() function for global registration, or it is enough to expose the json.dispatch_table mapping?
History
Date User Action Args
2017-03-05 10:03:59serhiy.storchakasetrecipients: + serhiy.storchaka, rhettinger, bob.ippolito, steven.daprano, r.david.murray, berker.peksag, abarry, Daniel Ward, Ollie Ford
2017-03-05 10:03:59serhiy.storchakasetmessageid: <1488708239.15.0.773031076559.issue27362@psf.upfronthosting.co.za>
2017-03-05 10:03:59serhiy.storchakalinkissue27362 messages
2017-03-05 10:03:56serhiy.storchakacreate