Skip to content

Commit

Permalink
Fixed #10240 -- Restored YAML serialization with Decimal objects that…
Browse files Browse the repository at this point in the history
… broke with r9823. Thanks Alex Gaynor for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9825 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
brosner committed Feb 12, 2009
1 parent e5cd43e commit e4e9645
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion django/core/serializers/pyyaml.py
Expand Up @@ -7,10 +7,21 @@
from StringIO import StringIO from StringIO import StringIO
import yaml import yaml


try:
import decimal
except ImportError:
from django.utils import _decimal as decimal # Python 2.3 fallback

from django.db import models from django.db import models
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer from django.core.serializers.python import Deserializer as PythonDeserializer


class DjangoSafeDumper(yaml.SafeDumper):
def represent_decimal(self, data):
return self.represent_scalar('tag:yaml.org,2002:str', str(data))

DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)

class Serializer(PythonSerializer): class Serializer(PythonSerializer):
""" """
Convert a queryset to YAML. Convert a queryset to YAML.
Expand All @@ -33,7 +44,7 @@ def handle_field(self, obj, field):
def end_serialization(self): def end_serialization(self):
self.options.pop('stream', None) self.options.pop('stream', None)
self.options.pop('fields', None) self.options.pop('fields', None)
yaml.safe_dump(self.objects, self.stream, **self.options) yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)


def getvalue(self): def getvalue(self):
return self.stream.getvalue() return self.stream.getvalue()
Expand Down

0 comments on commit e4e9645

Please sign in to comment.