<font face="arial,helvetica,sans-serif">The way it actually is now, there will be key collision in the dictionaries because each Foo has the same keys. I'm about to dive into the json docs for how it's done on complex, because that seems to be pretty much what I'm looking for.</font><div>

<font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif">And I was about to ask what top-posting was, but then I realized I wasn't sending this back to the list. So I'm going to assume what that was.</font></div>

<div><font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif">Thanks.<br></font><br><div class="gmail_quote">On Wed, Nov 3, 2010 at 11:42 AM, Chris Rebert <span dir="ltr"><<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">> On Wed, Nov 3, 2010 at 11:18 AM, Chris Rebert <<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>> wrote:<br>


>> > On Wed, Nov 3, 2010 at 10:35 AM, Chris Rebert <<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>> wrote:<br>
>> >> On Wed, Nov 3, 2010 at 8:30 AM, T.J. Simmons <<a href="mailto:theimmortalbum@gmail.com">theimmortalbum@gmail.com</a>><br>
>> >> wrote:<br>
>> >> > Hi all, got a question regarding serializing classes that I've<br>
>> >> > defined.<br>
>> >> > I<br>
>> >> > have some classes like<br>
>> >> > class Foo:<br>
>> >> >      def __init__(self, x, y):<br>
>> >> >           self.x = x, self.y = y<br>
>> >> > then a class that can contain multiple Foos, such as:<br>
>> >> > class Bar:<br>
>> >> >      def __init__(self):<br>
>> >> >           self.foos = [Foo(a, b), Foo(1, 2)]<br>
>> >> ><br>
>> >> > While that's a gross oversimplification of the real structure (it<br>
>> >> > gets<br>
>> >> > much,<br>
>> >> > much more nested than that), that's a pretty decent overview. The<br>
>> >> > actual<br>
>> >> > data for this is coming from a pseudo-XML file without any actual<br>
>> >> > structure,<br>
>> >> > so I wrote a parser according to the spec given to me, so I now have<br>
>> >> > all<br>
>> >> > the<br>
>> >> > data in a series of classes I've defined, with actual structure.<br>
>> >> > What I'm wanting to do is take this data I have and spit it out into<br>
>> >> > JSON,<br>
>> >> > but I really don't see a good way (I'm new to Python, this is my<br>
>> >> > first<br>
>> >> > real<br>
>> >> > project with it).<br>
>> >><br>
>> >> Did you google for "python json"? The std lib `json` module is the<br>
>> >> very first hit:<br>
>> >> <a href="http://docs.python.org/library/json.html" target="_blank">http://docs.python.org/library/json.html</a><br>
>><br>
>> On Wed, Nov 3, 2010 at 8:39 AM, T.J. Simmons <<a href="mailto:theimmortalbum@gmail.com">theimmortalbum@gmail.com</a>><br>
>> wrote:<br>
>> > Right, I know about the json module; that's not the problem. My problem<br>
>> > is<br>
>> > with the fact that different instances of the same class, with different<br>
>> > data, have the same keys. Foo, in this instance, can be both a list of<br>
>> > Foos<br>
>> > inside Bar, and also a list of Foos outside Bar. I'm just unsure of how<br>
>> > to<br>
>> > get the data into a serializable form.<br>
>><br>
>> So, if I'm understanding you correctly, your classes make use of<br>
>> dynamic typing and you think this will cause serialization problems?<br>
>> In that case, just define an appropriate JSONEncoder or object_hook;<br>
>> see the module docs, they give an example for complex numbers.<br>
>> If I've misunderstood you, a specific (pseudo-)code example of your<br>
>> problem would be helpful.<br>
<br>
</div></div><div class="im">On Wed, Nov 3, 2010 at 9:26 AM, T.J. Simmons <<a href="mailto:theimmortalbum@gmail.com">theimmortalbum@gmail.com</a>> wrote:<br>
> The issue with serialization is how I'm giving the data back to the<br>
> serializer, since I'm using dicts. Multiple dictionaries with a key of Foo<br>
> will overwrite each other, which isn't my desired behavior.<br>
<br>
</div>Eh? There should be no key collision. The *outer attribute's* name<br>
(e.g. "foos") should be the key, not the value's type's name (e.g.<br>
"Foo"). The type information for an object should go inside that<br>
object's own dict. Again, see the example for `complex` in the json<br>
module docs.<br>
<br>
Also, avoid top-posting in the future.<br>
<div><div></div><div class="h5"><br>
Cheers,<br>
Chris<br>
--<br>
<a href="http://blog.rebertia.com" target="_blank">http://blog.rebertia.com</a><br>
</div></div></blockquote></div><br></div>