diff options
| author | Brian Rosner <brosner@gmail.com> | 2009-02-12 02:55:32 +0000 |
|---|---|---|
| committer | Brian Rosner <brosner@gmail.com> | 2009-02-12 02:55:32 +0000 |
| commit | 8a89cf9d181aab833c2c9883c5de90482a04525c (patch) | |
| tree | f726c781fed2191578c0a29bbb16a67bcef1f20a /django/core | |
| parent | 5db8dc357323c1b87c8b110f25d3dbadeaa47c4e (diff) | |
[1.0.X] Fixed #10240 -- Restored YAML serialization with Decimal objects that broke with r9823. Thanks Alex Gaynor for the report.
Backport of r9825 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9826 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/serializers/pyyaml.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index ac77166a2f..34f8118d38 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -7,10 +7,21 @@ Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__. from StringIO import StringIO 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.core.serializers.python import Serializer as PythonSerializer 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): """ Convert a queryset to YAML. @@ -33,7 +44,7 @@ class Serializer(PythonSerializer): def end_serialization(self): self.options.pop('stream', 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): return self.stream.getvalue() |
