Re: Single-object serializers?
On 8/9/07, Shawn Allen <[EMAIL PROTECTED]> wrote: > > Hey everyone, > > I'm about to tackle an API that supports multiple response formats > using serializers. I realize that the API (as simple as it is) is > subject to change, but it seems like a totally sensible pattern for > what I'd like to do. One problem I've come across already is that the > base Serializer class's serialize() method assumes whatever is passed > to it is an iterable. This is a poor assumption in the case of my > API, which, in many cases, will be returning a serialized > representation of a single object. Can anyone recommend a strategy > for overriding this behavior? Adding serialization of single objects would be a reasonable addition to the Django serialization framework. There could be some devil in the details (especially on deserialization), but at a conceptual level, I don't see why we couldn't modify serialize() to check: if data is iterable: serialize list else serialize object Patches welcome. Yours, Russ Magee %-) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Single-object serializers?
Sorry... after reading that back to myself, I realize that the first sentence might be a little confusing: Just to clarify: I'm *building* an API that will output XML and JSON using Django serializers. I realize that the Django serialization API is subject to change. On Aug 8, 2007, at 12:41 PM, Shawn Allen wrote: > Hey everyone, > > I'm about to tackle an API that supports multiple response formats > using serializers. I realize that the API (as simple as it is) is > subject to change, but it seems like a totally sensible pattern for > what I'd like to do. One problem I've come across already is that the > base Serializer class's serialize() method assumes whatever is passed > to it is an iterable. This is a poor assumption in the case of my > API, which, in many cases, will be returning a serialized > representation of a single object. Can anyone recommend a strategy > for overriding this behavior? A couple of options I'm considering: > > - create iterable classes that represent "single object collections", > and generic classes to handle them > - register serialization format classes that do single object > serializations > > The latter could be particularly hairy because it requires copying a > lot of code from django.core.serializers.base.Serializer. The way > that serialize() is written now, at least, it's cleaner to create > classes that override start_serialization(), end_serialization(), and > all of the other methods called in between. > > Has anyone else come up with an elegant solution for this yet? > > Thanks, > Shawn > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Single-object serializers?
Hey everyone, I'm about to tackle an API that supports multiple response formats using serializers. I realize that the API (as simple as it is) is subject to change, but it seems like a totally sensible pattern for what I'd like to do. One problem I've come across already is that the base Serializer class's serialize() method assumes whatever is passed to it is an iterable. This is a poor assumption in the case of my API, which, in many cases, will be returning a serialized representation of a single object. Can anyone recommend a strategy for overriding this behavior? A couple of options I'm considering: - create iterable classes that represent "single object collections", and generic classes to handle them - register serialization format classes that do single object serializations The latter could be particularly hairy because it requires copying a lot of code from django.core.serializers.base.Serializer. The way that serialize() is written now, at least, it's cleaner to create classes that override start_serialization(), end_serialization(), and all of the other methods called in between. Has anyone else come up with an elegant solution for this yet? Thanks, Shawn --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---