Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Burak Arslan Newsgroups: comp.lang.python Subject: Re: New JSON encoding method proposal for custom objects Date: Tue, 1 Dec 2015 13:20:57 +0200 Lines: 62 Message-ID: References: <6f5cd8a5-6e14-4069-8dfe-4a1a00c05827@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ICE4l0kJcvyU7eqxreJ4LQAWhpSFwFkU0deYtbJ/DOfg== 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; 'xml,': 0.05; '21,': 0.07; 'default.': 0.07; 'encoder': 0.07; '(currently': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'ast': 0.09; 'instances.': 0.09; 'subject:method': 0.09; 'url:github': 0.09; 'example:': 0.10; 'python': 0.10; '55,': 0.16; 'anymore': 0.16; 'burak': 0.16; 'from:addr:arskom.com.tr': 0.16; 'from:addr:burak.arslan': 0.16; 'from:name:burak arslan': 0.16; 'json,': 0.16; 'message-id:@arskom.com.tr': 0.16; 'received:arskomhosting.com': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:JSON': 0.16; 'url:meta': 0.16; 'url:py': 0.16; 'yaml': 0.16; 'wrote:': 0.16; 'element': 0.18; 'integer': 0.18; '>>>': 0.20; 'skip:" 30': 0.20; 'to:2**1': 0.21; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'to:no real name:2**1': 0.27; '[2]': 0.27; 'workaround': 0.29; 'there.': 0.30; 'code': 0.30; 'skip:g 30': 0.30; 'skip:[ 10': 0.31; 'everyone': 0.31; '[1]': 0.32; 'getting': 0.33; 'class': 0.33; 'skip:j 20': 0.33; 'similar': 0.33; 'definition': 0.34; 'skip:d 20': 0.34; 'best,': 0.35; 'could': 0.35; 'i.e.': 0.35; 'propose': 0.35; 'returning': 0.35; 'unicode': 0.35; 'should': 0.36; 'totally': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; '12,': 0.37; 'turned': 0.38; 'thank': 0.38; 'format': 0.39; 'whatever': 0.39; 'to:addr:python.org': 0.40; 'care': 0.60; 'your': 0.60; 'default': 0.61; 'email addr:gmail.com': 0.62; 'yes': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'different': 0.63; 'received:176': 0.66; 'integrated': 0.72; 'hey,': 0.75; 'url:10': 0.79; 'right!': 0.84; 'worry,': 0.84; 'imagine': 0.96 X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <6f5cd8a5-6e14-4069-8dfe-4a1a00c05827@googlegroups.com> 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:99791 hey, On 11/30/15 14:35, cescus92@gmail.com wrote: > > Hello everyone and thank you for your interest! > > The Peter's code is very similar to what I think the default JSON encoder should be. > > The advantage of the method that I propose is that you should not care anymore about which encoder you're going to use even in case of different class instances. Imagine if you could just do > > json.dumps({[1,2,3], Obj(), [DifferentObj()] }) > > FWIW, Spyne can to the exact same thing -- i.e. serialize an object given its definition to whatever format you want. (currently xml, json, yaml and msgpack are supported). Here's a json example: >>> from spyne import * >>> from spyne.util.dictdoc import get_object_as_json, get_json_as_object >>> get_object_as_json([1,2,3], Array(Integer)) '[1, 2, 3]' >>> >>> from datetime import datetime >>> >>> class SomeObject(ComplexModel): ... s = Unicode ... i = Integer ... dt = DateTime ... >>> get_object_as_json(SomeObject(s='str', i=42, dt=datetime.now()), complex_as=list) '[42, "str", "2015-12-01T12:57:23.751631"]' >>> >>> get_json_as_object('[42, "str", "2015-12-01T12:55:21.777108"]', SomeObject, complex_as=list) SomeObject(i=42, s=u'str', dt=datetime.datetime(2015, 12, 1, 12, 55, 21, 777108)) >>> >>> More info: http://spyne.io Best, Burak PS: The astute reader will notice that element order in SomeObject could be totally random. * In Python 3, we solve that by returning an odict() in the __prepare__ of the ComplexModel metaclass. * In Python 2, we solve that by somehow getting hold of AST of the class definition and deducing the order from there. Yes you read that right! I know, it's horrible! Don't worry, it's turned off by default. We recommend the workaround in [1] for Python 2. See [2] and [3] to see how we integrated it. [1]: http://spyne.io/docs/2.10/manual/03_types.html#complex [2]: https://github.com/arskom/spyne/pull/313 [3]: https://github.com/arskom/spyne/blob/2768c7ff0b5f58aa0e47859fcd69e5bb7aa31aba/spyne/util/meta.py