Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Implement Serializer class
Browse files Browse the repository at this point in the history
This provides the ability to encapsulate a set of default options
for a particular serialization use case.

Signed-off-by: Byron Ruth <b@devel.io>
  • Loading branch information
bruth committed Feb 24, 2016
1 parent f7f78e7 commit cd5d929
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions preserialize/serialize.py
Expand Up @@ -21,6 +21,13 @@
}


def _merge(target, *sources):
for source in sources:
target.update(source)

return target


def _defaults(options):
if 'key_map' in options and 'aliases' not in options:
warnings.warn('The "key_map" option has been renamed to "aliases"',
Expand Down Expand Up @@ -146,32 +153,40 @@ def queryset_to_list(queryset, **options):
return [model_to_dict(x, **options) for x in queryset]


def serialize(obj, fields=None, exclude=None, **options):
"""Recursively attempts to find ``Model`` and ``QuerySet`` instances
to convert them into their representative datastructure per their
``Resource`` (if one exists).
"""
class Serializer(object):
def __init__(self, **options):
self.options = options

def serialize(self, obj, fields=None, exclude=None, **options):
"""Recursively attempts to find ``Model`` and ``QuerySet`` instances
to convert them into their representative datastructure per their
``Resource`` (if one exists).
"""
options = _merge({}, self.options, options)

# Handle model instances
if isinstance(obj, models.Model):
fields = parse_selectors(obj.__class__, fields, exclude, **options)
return model_to_dict(obj, fields=fields, **options)

# Handle querysets
if isinstance(obj, QuerySet):
fields = parse_selectors(obj.model, fields, exclude, **options)
return queryset_to_list(obj, fields=fields, **options)

# Handle model instances
if isinstance(obj, models.Model):
fields = parse_selectors(obj.__class__, fields, exclude, **options)
return model_to_dict(obj, fields=fields, **options)
# Handle dict instances
if isinstance(obj, dict):
exclude = exclude or []
if not fields:
fields = iter(obj.keys())
fields = [x for x in fields if x not in exclude]
return model_to_dict(obj, fields=fields, **options)

# Handle querysets
if isinstance(obj, QuerySet):
fields = parse_selectors(obj.model, fields, exclude, **options)
return queryset_to_list(obj, fields=fields, **options)
# Handle other iterables
if hasattr(obj, '__iter__'):
return [self.serialize(x, fields, exclude, **options) for x in obj]

# Handle dict instances
if isinstance(obj, dict):
exclude = exclude or []
if not fields:
fields = iter(obj.keys())
fields = [x for x in fields if x not in exclude]
return model_to_dict(obj, fields=fields, **options)
return obj

# Handle other iterables
if hasattr(obj, '__iter__'):
return [serialize(x, fields, exclude, **options) for x in obj]

return obj
serialize = Serializer().serialize

0 comments on commit cd5d929

Please sign in to comment.