Hello,

I'm not really sure how suggesting improvements to Python goes and I've found this mailing list, so I thought I'd email it.

My suggestion is related to the json built in package, and more exactly about serialising custom classes.

As of now, the process involves using a JSONEncoder class which, by default, supports the following types/values: dict, list, tuple, str, int, float, True, False, None and int- & float-derived Enums.

In order to dump a custom class to JSON, the process involves creating a new class inheriting JSONEncoder and defining the default function in order to handle custom classes.

I say that this approach is very complex and perhaps unpythonic, definitely very weird compared to other approaches.

An alternative that comes to mind and would feel more natural is adding support for recognising a __json__ function. If a class would contain such a function, the default JSONEncoder should take that function's return value instead for encoding, assuming that such returned value will be able to be then serialised.

If such a functionality would be added, many custom functions will most likely return a dict from their __json__ function, skipping the need for creating a class inheriting JSONEncoder altogether.

I hope this idea is a good one and could perhaps even get implemented in the next version of Python. 

Respectfully,
Dan Cojocaru.