Serializing a user-defined class

Chris Rebert clp2 at rebertia.com
Wed Nov 3 17:43:20 CET 2010


> On Wed, Nov 3, 2010 at 11:18 AM, Chris Rebert <clp2 at rebertia.com> wrote:
>> > On Wed, Nov 3, 2010 at 10:35 AM, Chris Rebert <clp2 at rebertia.com> wrote:
>> >> On Wed, Nov 3, 2010 at 8:30 AM, T.J. Simmons <theimmortalbum at gmail.com>
>> >> wrote:
>> >> > Hi all, got a question regarding serializing classes that I've
>> >> > defined.
>> >> > I
>> >> > have some classes like
>> >> > class Foo:
>> >> >      def __init__(self, x, y):
>> >> >           self.x = x, self.y = y
>> >> > then a class that can contain multiple Foos, such as:
>> >> > class Bar:
>> >> >      def __init__(self):
>> >> >           self.foos = [Foo(a, b), Foo(1, 2)]
>> >> >
>> >> > While that's a gross oversimplification of the real structure (it
>> >> > gets
>> >> > much,
>> >> > much more nested than that), that's a pretty decent overview. The
>> >> > actual
>> >> > data for this is coming from a pseudo-XML file without any actual
>> >> > structure,
>> >> > so I wrote a parser according to the spec given to me, so I now have
>> >> > all
>> >> > the
>> >> > data in a series of classes I've defined, with actual structure.
>> >> > What I'm wanting to do is take this data I have and spit it out into
>> >> > JSON,
>> >> > but I really don't see a good way (I'm new to Python, this is my
>> >> > first
>> >> > real
>> >> > project with it).
>> >>
>> >> Did you google for "python json"? The std lib `json` module is the
>> >> very first hit:
>> >> http://docs.python.org/library/json.html
>>
>> On Wed, Nov 3, 2010 at 8:39 AM, T.J. Simmons <theimmortalbum at gmail.com>
>> wrote:
>> > Right, I know about the json module; that's not the problem. My problem
>> > is
>> > with the fact that different instances of the same class, with different
>> > data, have the same keys. Foo, in this instance, can be both a list of
>> > Foos
>> > inside Bar, and also a list of Foos outside Bar. I'm just unsure of how
>> > to
>> > get the data into a serializable form.
>>
>> So, if I'm understanding you correctly, your classes make use of
>> dynamic typing and you think this will cause serialization problems?
>> In that case, just define an appropriate JSONEncoder or object_hook;
>> see the module docs, they give an example for complex numbers.
>> If I've misunderstood you, a specific (pseudo-)code example of your
>> problem would be helpful.

On Wed, Nov 3, 2010 at 9:26 AM, T.J. Simmons <theimmortalbum at gmail.com> wrote:
> The issue with serialization is how I'm giving the data back to the
> serializer, since I'm using dicts. Multiple dictionaries with a key of Foo
> will overwrite each other, which isn't my desired behavior.

Eh? There should be no key collision. The *outer attribute's* name
(e.g. "foos") should be the key, not the value's type's name (e.g.
"Foo"). The type information for an object should go inside that
object's own dict. Again, see the example for `complex` in the json
module docs.

Also, avoid top-posting in the future.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list