Allow me to provide an example that I have just created and confirmed works
with Python 2.6.6 on Ubuntu 10.10.

-----<cut here>-----
import json
class Order(object):
    def __init__(self, oid=None, title=None):
        self.oid=oid
        self.title=title

    def __repr__(self):
        return "(oid=%s, title=%s)" % (self.oid, self.title)

def orderdecode(dct):
    if 'orderID' in dct:
        return Order(dct['orderID'], dct['moduleTitle'])
    return dct

order = '[{"orderID": 123334456, "moduleTitle": "Public/Financial D&O
Liability Insurance"}]'

print json.loads(order, object_hook=orderdecode)
-----<cut here>-----

When you run this code, you will get a list (only 1 element long) that
contains an Order object. That order object will have the same data as the
order string. I wrote it using the examples provided at
http://docs.python.org/library/json.html as my basis. Search the page for
"Specializing JSON object decoding".

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?

On Fri, Apr 8, 2011 at 6:21 AM, Alessandro Molina <
[email protected]> wrote:

> On Fri, Apr 8, 2011 at 12:07 PM, Daniel Fetchinson
> <[email protected]> wrote:
> > If you have a json object, you probably got it from a python object
> > which was json-able and you serialized it using the json/simplejson
> > module.
>
> That is indeed not actually true, you might be getting the JSON from
> any remote source or different software.
>
> JSONEncode by itself can only encode/decode base types not any kind of
> object.
> I understood that he wants to encode/decode his own objects, which
> would required to subclass the .default method of the JSONEncoder or
> find a way to extract the object as a dict.
>
> > If this is so, what stops the OP from using the
> > json/simplejson module to decode back to python?
> >
>
> As far as I understood his only problem is actually that he has to
> serialize/deserialize objects instead of base types.
>
> --
> 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.

Reply via email to