#3564: flexible JSON serializing mechanism
-----------------------------------+----------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: jacob
Status: new | Component: Serialization
Version: SVN | Resolution:
Keywords: | Stage: Unreviewed
Has_patch: 0 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
-----------------------------------+----------------------------------------
Changes (by [EMAIL PROTECTED]):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
After some more research (and now some working code for this) I came to
the conclusion that what is needed is something like this:
{{{
class DjangoJSONEncoder(DateTimeAwareJSONEncoder):
"""
DateTimeAwareJSONEncoder subclass that knows how to forward encoding
to the object if it has the method json_serialize()
"""
def encode(self, o):
from django.db.models.query import QuerySet
if isinstance(o, QuerySet):
o = list(o)
return super(DjangoJSONEncoder, self).encode(o)
def default(self, o):
try:
return o.json_serialize()
except AttributeError:
return super(DjangoJSONEncoder, self).default(o)
}}}
This still leads to some rather messy code in the model, but it is at
least an improvement. A better system would be for a marker class like
Admin, so you can do "class JSON:pass" and then you get some reasonable
default for these things.
--
Ticket URL: <http://code.djangoproject.com/ticket/3564#comment:1>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---