Message289007
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? |
|
Date |
User |
Action |
Args |
2017-03-05 10:03:59 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, rhettinger, bob.ippolito, steven.daprano, r.david.murray, berker.peksag, abarry, Daniel Ward, Ollie Ford |
2017-03-05 10:03:59 | serhiy.storchaka | set | messageid: <1488708239.15.0.773031076559.issue27362@psf.upfronthosting.co.za> |
2017-03-05 10:03:59 | serhiy.storchaka | link | issue27362 messages |
2017-03-05 10:03:56 | serhiy.storchaka | create | |
|