Now that I've read your take on the question, I understand why you think the way you do.
Okay, so back to the OP time: Which question are you asking? Do you need help with deserializing JSON data into a set of classes you define internally, or are you looking for help with deserializing classes that can come in from outside that would best be handled by pickle? On Fri, Apr 8, 2011 at 8:28 AM, Alessandro Molina < [email protected]> wrote: > On Fri, Apr 8, 2011 at 1:29 PM, Michael Pedersen <[email protected]> > wrote: > > print json.loads(order, object_hook=orderdecode) > > > > All of which is a roundabout way of saying the same question again: > What's > > wrong with using the features provided by the json/simplejson libraries? > Is > > it just a case of "didn't know" or is there some other reason we don't > know > > about? > > I'm starting to think that I misunderstood his request :) > You will offcourse end up using json/simplejson to decode/encode json > > But I understood that he wanted to deserialize generic objects whose > type he knows nothing. > In this case I suggested to use the pickling protocol to have a > dumpable representation of the object. > > try: > getstate = obj.__getstate__ > except AttributeError: > stuff = obj.__dict__ > else: > stuff = getstate() > return json.dumps(stuff) > > Should dump the object and then doing something like: > > class UnmarshalledObject: > pass > state = json.loads(...) > inst = UnmarshalledObject() > inst.__dict__.update(state) > > Should load the object back. > UnmarshalledObject is pratically the same as Bunch and forgets the > real object type. Can be improved to restore the real type. > > Off course if you already know that you want to map orderID to > Order.oid and moduleTitle to Order.title this is quite useless. > > It is pratically the same difference that you have between json and > pickle. The first can serialize/deserialize only things that knows > about while the seconds can serialize pratically anything that can be > instanced in python. > > -- > You received this message because you are subscribed to the Google Groups > "TurboGears" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/turbogears?hl=en. > > -- Michael J. Pedersen My IM IDs: Jabber/[email protected], ICQ/103345809, AIM/pedermj022171 Yahoo/pedermj2002, MSN/[email protected] ---------- All humans fail, in both great and small ways we fail continually. Machines fail too. Computers are machines that are managed by humans, the fallout from failure can be spectacular. Your responsibility is to deal with failure, to anticipate it and to eliminate it as far as is humanly and economically wise to achieve. Are your actions part of the problem or part of the solution? -- You received this message because you are subscribed to the Google Groups "TurboGears" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

