JSONResponseMixin: Best handle "context" before dumping it to JSON?
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?
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home