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


Groups > comp.lang.python > #99791

Re: New JSON encoding method proposal for custom objects

From Burak Arslan <burak.arslan@arskom.com.tr>
Newsgroups comp.lang.python
Subject Re: New JSON encoding method proposal for custom objects
Date 2015-12-01 13:20 +0200
Message-ID <mailman.67.1448969416.14615.python-list@python.org> (permalink)
References <aac1b6e8-e294-4916-9485-ffc031c3aeb3@googlegroups.com> <n3fnru$5sf$3@dont-email.me> <mailman.32.1448873773.14615.python-list@python.org> <6f5cd8a5-6e14-4069-8dfe-4a1a00c05827@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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