Re: JSONResponseMixin: Best handle "context" before dumping it to JSON?
On Wed, Dec 12, 2012 at 3:21 AM, Micky Hulse <rgmicky@gmail.com> wrote:
Hello,
I'm modifying a version of this code (JSONResponseMixin):
<https://docs.djangoproject.com/en/1.3/topics/class-based-views/#more-than-just-html>
... so it can handle jsonp and caching (so far so good)... I was just
curious, the comments say:
<snip>
# Note: This is *EXTREMELY* naive; in reality, you'll need
# to do much more complex handling to ensure that arbitrary
# objects -- such as Django model instances or querysets
# -- can be serialized as JSON.
return json.dumps(context)
<snip>
Question: I'm just wondering if anyone could provide some examples of
non-naive handling?
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.
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.
Error handling really doesn't require anything more than catching the exception if the encoder fails.
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.
Yours,
Russ Magee %-)
Should I iterate over the context and check the type to make sure it's
serialize (if not, try to serialize or throw an error)?
So far, I've found that I do most of my checking in my view (not the mixin):
<snip>
class Api(JSONResponseMixin, BaseDetailView):
def get(self, request, *args, **kwargs):
# Basic checking here, before rendering "data" list to response...
return self.render_to_response(data)
</snip>
Going back to my question: I can live with what I have now, but I'd
like to make my code as robust as possible... If someone could give me
some pointers on how to best handle "context" before dumping it to
JSON, I'd appreciate it. :)
Have a nice day!
Cheers,
M
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home