<div dir="ltr"><div class="m_-5050394239199177574inbox-inbox-F0XO1GC-ed-a" style="margin:0px 0px 0px 44px;padding:0px 0px 0px 5px;border:0px"><div class="m_-5050394239199177574inbox-inbox-F0XO1GC-nb-P" style="margin:0px;padding:0px;border:0px;line-height:normal"><div style="margin:0px;padding:0px;border:0px"><div style="margin:0px;padding:0px;border:0px;overflow:auto"><div style="margin:0px;padding:0px;border:0px;max-height:10000px"><div dir="ltr" style="margin:0px;padding:0px;border:0px"><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Hi!</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Most of the classes (even if very simple like datetime.datetime) cannot be serialized to JSON by default.</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Would it be a good idea for the default json.JSONEncoder to call the __json__ dunder method automatically if the object has one?</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">I can't find anything about why this protocol or PEP doesn't exists yet.</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Currently almost everyone implements it like this, there are thousands of results on GitHub to this:</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><a href="https://github.com/search?l=Python&q=def+__json__&type=Code&utf8=%E2%9C%93" target="_blank">https://github.com/search?l=Python&q=def+__json__&type=Code&utf8=%E2%9C%93</a></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">but there is no canonical way (Python/standard way) to do this.</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">It would be very nice, because a custom JSONEncoder would not be needed and </font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">everyone could implement JSON serialization in One True Way (yay for code reuse!)</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">The implementation could be very simple, would look something like this:</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">class JSONEncoder:</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">    def default(self, obj):</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">        if hasattr(obj, '__json__'):</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">            return obj.__json__()</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">        return current_implementation</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Or to make it easier even for decoding, it could be __to_json__ and __from_json__</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">or __json_encode__ and __json_decode__ or something like these.</font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">Was there already a PEP/discussion about this?</font></div></div></div></div></div></div></div><div dir="ltr"><div class="m_-5050394239199177574inbox-inbox-F0XO1GC-ed-a" style="margin:0px 0px 0px 44px;padding:0px 0px 0px 5px;border:0px"><div class="m_-5050394239199177574inbox-inbox-F0XO1GC-nb-P" style="margin:0px;padding:0px;border:0px;line-height:normal"><div style="margin:0px;padding:0px;border:0px"><div style="margin:0px;padding:0px;border:0px;overflow:auto"><div style="margin:0px;padding:0px;border:0px;max-height:10000px"><div dir="ltr" style="margin:0px;padding:0px;border:0px"><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222"><br></font></div><div dir="ltr" style="margin:0px;padding:0px;border:0px"><font color="#222222">György</font></div><div style="color:rgb(34,34,34)"><br></div></div></div></div></div></div></div></div></div>