Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: New JSON encoding method proposal for custom objects Date: Sun, 29 Nov 2015 16:46:13 +0100 Organization: None Lines: 98 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de ayBI3KiZUv0uEGOeoRhXjwZgt3US7Gmxj+hl4uyiqV+w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'json': 0.05; 'converts': 0.07; 'method,': 0.07; 'ugly': 0.07; 'encode': 0.09; 'lines:': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'str,': 0.09; 'subject:method': 0.09; 'python': 0.10; 'def': 0.13; 'code?': 0.16; 'decoding': 0.16; 'enough.': 0.16; 'instance:': 0.16; 'mechanism.': 0.16; 'need,': 0.16; 'one-way': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:JSON': 0.16; 'task:': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'mechanism': 0.18; 'pace': 0.18; 'transform': 0.18; 'try:': 0.18; 'programmer': 0.18; '>>>': 0.20; 'not,': 0.22; 'everyone!': 0.22; 'object.': 0.22; 'simpler': 0.22; "python's": 0.23; 'import': 0.24; 'discussion': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'header:X-Complaints- To:1': 0.26; 'error': 0.27; 'least': 0.27; 'defining': 0.27; 'idea': 0.28; 'looks': 0.29; 'pep': 0.29; 'preliminary': 0.29; 'unified': 0.29; 'convert': 0.29; "i'm": 0.30; 'e.g.': 0.30; 'task': 0.30; "i'd": 0.31; 'extend': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'implement': 0.32; 'non': 0.32; 'class': 0.33; 'source': 0.33; 'common': 0.33; 'instances': 0.33; 'case,': 0.34; 'quickly': 0.34; 'except': 0.34; 'handle': 0.34; 'list': 0.34; 'advice': 0.35; 'ones': 0.35; 'could': 0.35; 'instance': 0.35; 'path': 0.35; 'propose': 0.35; 'requiring': 0.35; 'item': 0.35; 'community': 0.36; 'list,': 0.36; 'should': 0.36; 'needed': 0.36; 'there': 0.36; 'guidance': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'received:org': 0.37; 'thought': 0.37; 'wanted': 0.37; 'feedback': 0.38; 'thank': 0.38; 'or,': 0.38; 'why': 0.39; 'goes': 0.39; 'data': 0.39; 'sure': 0.39; "didn't": 0.39; 'enough': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'waiting': 0.60; 'care': 0.60; 'your': 0.60; 'personally': 0.61; 'email addr:gmail.com': 0.62; 'matter': 0.63; 'more': 0.63; 'our': 0.64; 'here': 0.66; 'day': 0.67; 'duty': 0.72; 'treat': 0.72; 'actually,': 0.84; 'complexity': 0.84; 'dict,': 0.84; 'elementary': 0.84; 'hurry,': 0.84; 'glad': 0.87; 'why?': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8f47.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99696 cescus92@gmail.com wrote: > Hello everyone! > > I'm writing here since I've read on the Pyhton's documentation that this > is the most common path that a new proposal should follow. I'd be really > glad if this proposal could become a PEP if I see a good response from the > community or, in the worst case, understand why this proposal is not good > enough. > > In this day I stumbled upon a very simple task: I had a list of instances > of a custom class and I had to convert i into a JSON. > > Actually, from what I've discovered, there are 2 ways to get the JSON out > of a custom non serializable instance: - defining a new method ( e.g. > to_json() ) inside the class that converts the item manually (quicker) - a > custom encoder/decoder that goes to extend the ones from the json lib > (cleander, I think) > > Since I thought it was the most cost-effective and I was in hurry, I took > the first way that drove me to a "problem": if I wanted to get the JSON > out from my list, it obviously would have given me the "non serializable > object" error since the json lib didn't know how to transform it into > JSON! > > I was astonished that Python was requiring me such ugly ways to accomplish > this simple task! JSON WebServices are becoming more and more popular and > Python _must_ keep the pace of time! And here it is my proposal to make > this task easier in the best Pythonic way :) > > I propose that every custom class that wants to allow its instances to > have a JSON representation could implement a simple method, __json__(). > > The way how it works it very straightforward: every time json lib is > required to convert an object that doesn't know, it looks for the > __json__() method to learn how to treat the object. The duty of this > method should be to reduce the complexity of an object instance into an > elementary data type (int, str, list, dict, etc..). > > Often the process to get the JSON out of an object is a one-way need, in > this way it would become even simpler to do it in the most flawless way so > the programmer should no more care to implement some custom logic to > handle quickly these situations. > > Let me give you a very small example of what a __json__ method could be > like: > > class CustomObj(): > def __init__(self, p1, p2): > self.p1 = p1 > self.p2 = p2 > > def __json__(self): > return { > 'p1': self.p1, > 'p2': self.p2 > } > > Once more, the job of __json__ should simply be the one of make our object > json-readable :) > > What do you think of it? Do you think it's good? If not, why? I'm not sure if that is needed often enough so that adding yet another __dunder__ method is worthwhile, particularly as the current mechanism already makes an implementation of your idea a matter of a few lines: >>> import json >>> class Encoder(json.JSONEncoder): ... def default(self, obj): ... try: encode = obj.__json__ ... except AttributeError: return super().default(obj) ... return encode() ... >>> class Foo: ... def __json__(self): return dict(bar=1, baz=2) ... >>> json.dumps({1:2, 3:Foo()}, cls=Encoder) '{"1": 2, "3": {"baz": 2, "bar": 1}}' Personally I would find it more interesting if there were a unified mechanism for json and pickle. At the very least you should come up with a decoding mechanism. > Do you have any guidance to make this idea advance? Go to python-ideas for a lengthy discussion ;) > Do you advice me to write down a preliminary PEP or to make directly a > pull request to the Python's source code? > > Thank you for you're time! > I'm waiting for your feedback :) > Have a good Sunday! > > Francesco