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.

Reply via email to