Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99695

New JSON encoding method proposal for custom objects

X-Received by 10.129.125.4 with SMTP id y4mr52381004ywc.48.1448809531341; Sun, 29 Nov 2015 07:05:31 -0800 (PST)
X-Received by 10.50.128.161 with SMTP id np1mr38340igb.4.1448809531313; Sun, 29 Nov 2015 07:05:31 -0800 (PST)
Path csiph.com!au2pb.net!feeder.erje.net!2.us.feeder.erje.net!news.ripco.com!news.glorb.com!b51no4279134qgf.0!news-out.google.com!f6ni14392igq.0!nntp.google.com!mv3no5535137igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sun, 29 Nov 2015 07:05:30 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=62.10.88.244; posting-account=T1DhlQoAAACm4133jFhuKMInTsDNovhQ
NNTP-Posting-Host 62.10.88.244
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <aac1b6e8-e294-4916-9485-ffc031c3aeb3@googlegroups.com> (permalink)
Subject New JSON encoding method proposal for custom objects
From cescus92@gmail.com
Injection-Date Sun, 29 Nov 2015 15:05:31 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:99695

Show key headers only | View raw


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?

Do you have any guidance to make this idea advance?

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

New JSON encoding method proposal for custom objects cescus92@gmail.com - 2015-11-29 07:05 -0800
  Re: New JSON encoding method proposal for custom objects Peter Otten <__peter__@web.de> - 2015-11-29 16:46 +0100
  Re: New JSON encoding method proposal for custom objects Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-29 20:40 +0000
    Re: New JSON encoding method proposal for custom objects Marco Kaulea <marco.kaulea@gmail.com> - 2015-11-30 09:42 +0100
      Re: New JSON encoding method proposal for custom objects cescus92@gmail.com - 2015-11-30 04:35 -0800
        Re: New JSON encoding method proposal for custom objects Burak Arslan <burak.arslan@arskom.com.tr> - 2015-12-01 13:20 +0200
          Re: New JSON encoding method proposal for custom objects cescus92@gmail.com - 2015-12-01 04:08 -0800
        Re: New JSON encoding method proposal for custom objects Peter Otten <__peter__@web.de> - 2015-12-01 13:17 +0100
          Re: New JSON encoding method proposal for custom objects cescus92@gmail.com - 2015-12-01 04:24 -0800

csiph-web