Hello Russ! Thank you so much for your pro help and informative reply.
I really appreciate it. :)

Sorry for my late reply... A few non-Django projects jumped in the way
of me tending to my list e-mails.

See my replies, inline, below:

On Tue, Dec 11, 2012 at 3:51 PM, Russell Keith-Magee
<russ...@keith-magee.com> wrote:
> It's a little hard to provide an example here without knowing exactly what
> is being serialised -- that's why the example in the docs is deliberately
> naive.
> json.dumps() will handle almost all the datatypes that come out of the box
> with Python. However, as soon as you have non-default types -- querysets,
> models, or anything else -- the default handling falls over. Exactly what
> handling is required depends on what non-default types exist in the content
> you want to serialize.

Ah, I see. Now that you point it out, I understand now why the docs
left out those details.

Not sure if this will help, but here's the code that creates the data
I end up dumping to JSON:

<https://github.com/registerguard/django-ad-manager/blob/master/ad_manager/views.py#L148-L180>

(Note to future readers: those lines may move in a few days as I tweak
the code.)

Basically, I'm just getting string values from my table and not doing
anything special and/or handling any non-default types.

And just for completeness' sake, the JSON mixin is here:

<https://github.com/registerguard/django-ad-manager/blob/master/ad_manager/mixins.py>

... specifically, this line:

<https://github.com/registerguard/django-ad-manager/blob/master/ad_manager/mixins.py#L84>

(IBID)

... it's pretty much straight from the docs.

> Error handling really doesn't require anything more than catching the
> exception if the encoder fails.

I've been wondering if I could just add a simple try/catch, like this code here:

<https://github.com/registerguard/django-ad-manager/blob/57f77dd0b5b59351ad50629f72c9fbb2083b1dd7/ad_manager/decorators.py#L37-L44>

<snip>

try:
    data = simplejson.dumps(objects)
except:
    data = simplejson.dumps(str(objects))

</snip>

Note where str() is being used... Though, my co-worker has told me
that using "except" without an argument is bad style and frowned upon.

I'm sure once I've read through the links you've provided, I'll be on
the right track... The above try/catch solution seems a little too
simple.

> The solution will usually come in the form of a custom encoder class.
> Python's docs provide an example of how this can be done [1]; obviously,
> you'll need to adapt this to whatever data types you need to serialize.
> The other option is to use Django's serializers [2]; this mechanism includes
> built-in support for serialising querysets and objects, which removes the
> need for a lot of special handling.

Ah, great info! Thank you for pointing me in the right direction. I
plan on reading through these docs tonight.

Much appreciated and have a great night!

Cheers,
Micky

-- 
Micky Hulse
Web Content Editor
The Register-Guard
3500 Chad Drive
Eugene, OR 97408
Phone: (541) 338-2621
Fax: (541) 683-7631
Web: <http://www.registerguard.com>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to