Skip to content

Commit

Permalink
Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Dec 18, 2014
1 parent 6da39ee commit df76291
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rest_framework_yaml/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@

from django.utils import six

# Note: ReturnDict and ReturnList are private API
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList

from .compat import yaml, OrderedDict


# Adapted from http://pyyaml.org/attachment/ticket/161/use_ordered_dict.py
class SafeDumper(yaml.SafeDumper):
"""
Handles decimals as strings.
Handles OrderedDicts as usual dicts, but preserves field order, rather
than the usual behaviour of sorting the keys.
Adapted from http://pyyaml.org/attachment/ticket/161/use_ordered_dict.py
"""
def represent_decimal(self, data):
return self.represent_scalar('tag:yaml.org,2002:str', six.text_type(data))
Expand Down Expand Up @@ -55,6 +59,16 @@ def represent_mapping(self, tag, mapping, flow_style=None):
yaml.representer.SafeRepresenter.represent_dict
)

SafeDumper.add_representer(
ReturnDict,
yaml.representer.SafeRepresenter.represent_dict
)

SafeDumper.add_representer(
ReturnList,
yaml.representer.SafeRepresenter.represent_list
)

SafeDumper.add_representer(
types.GeneratorType,
yaml.representer.SafeRepresenter.represent_list
Expand Down

0 comments on commit df76291

Please sign in to comment.