Serhiy Storchaka added the comment:

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?

----------
keywords: +patch
Added file: http://bugs.python.org/file46701/json-customize.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27362>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to