Re: [pyxser-users] problem serializing built-in and other types
Brought to you by:
damowe
From: Daniel M. W. <dm...@co...> - 2010-08-23 00:51:36
|
On Sunday 22 August 2010, Vardan Akopian <vak...@gm...> wrote: > Hi Daniel, > > It looks like pyxser has a problem serializing built-in types when > they are at the top level. For example each of the following will > throw a "ValueError: Argument given for serialization/deserialization > is not a Python Object or is None.": > > pyxser.serialize(obj = 1, enc = 'utf-8') > pyxser.serialize(obj = 'foo', enc = 'utf-8') > pyxser.serialize(obj = ['foo'], enc = 'utf-8') > pyxser.serialize(obj = ('foo'), enc = 'utf-8') > pyxser.serialize(obj = {1:'foo'}, enc = 'utf-8') pyxser, pyxser XML schema and pyxser XML DTD are designed to serialize objects with a constructor. Since those types of objects do not have an accesible constructor from the C/API, I can't deserialize those objects and pyxser can't deserialize those kinds of objects. > > Also, an empty class also has the same problem: > class Test: pass > pyxser.serialize(obj = Test(), enc = 'utf-8') The same happens here. Those objects require a defined constructor accesible from the Python C/API. The Python C/API just provides me of PyInstance_NewRaw() for objects with argumented constructors and PyObject_CallFunctionObjArgs() for object with non-argumented constructor. If you read the documentation you will notice that is not possible to restore those instances. > > But if I do > x = Test() > x.foo = 'foo' > pyxser.serialize(obj = x, enc = 'utf-8') > > then it works fine. > > Is this by design, or am I using it wrongly? > I encountered this when I was trying to serialize the result of > User.all() that gets all the users through sqlalchemy. Yep, that works. If you want to serialize a list, like the User.all() is returning, I suggest that you must use a dummy object: x = Test() x.foo = User.all() pyxser.serialize(obj = x, enc = 'utf-8') This will work :) > > Thanks. > -Vardan Best regards, -- Daniel Molina Wegener <dmw [at] coder [dot] cl> System Programmer & Web Developer Phone: +56 (2) 979-0277 | Blog: http://coder.cl/ |