summaryrefslogtreecommitdiff
path: root/django/core/serializers/pyyaml.py
blob: b7bfa9d54e3153a029d4beeba99325c97deeb244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
YAML serializer.

Requires PyYaml (https://pyyaml.org/), but that's checked for in __init__.
"""

import datetime
import decimal

import yaml

from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Deserializer as PythonDeserializer
from django.core.serializers.python import Serializer as PythonSerializer

# Use the C (faster) implementation if possible
try:
    from yaml import CSafeDumper as SafeDumper
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeDumper, SafeLoader


class DjangoSafeDumper(SafeDumper):
    # The "safe" serializer is used for better interoperability.

    def represent_decimal(self, data):
        return self.represent_scalar("tag:yaml.org,2002:str", str(data))

    def represent_time(self, data):
        # Base YAML doesn't support serialization of time types (as opposed to
        # dates or datetimes, which it does support). Converting them to
        # strings isn't perfect, but it's better than a "!!python/time" type
        # which would prevent deserialization under any other language.
        return self.represent_scalar("tag:yaml.org,2002:str", str(data))


DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
DjangoSafeDumper.add_representer(datetime.time, DjangoSafeDumper.represent_time)


class Serializer(PythonSerializer):
    """Convert a queryset to YAML."""

    internal_use_only = False

    def end_serialization(self):
        self.options.setdefault("allow_unicode", True)
        yaml.dump(
            self.objects,
            self.stream,
            Dumper=DjangoSafeDumper,
            sort_keys=False,
            **self.options,
        )

    def getvalue(self):
        # Grandparent super
        return super(PythonSerializer, self).getvalue()


class Deserializer(PythonDeserializer):
    """Deserialize a stream or string of YAML data."""

    def __init__(self, stream_or_string, **options):
        stream = stream_or_string
        if isinstance(stream_or_string, bytes):
            stream = stream_or_string.decode()
        try:
            objects = yaml.load(stream, Loader=SafeLoader)
        except Exception as exc:
            raise DeserializationError() from exc
        super().__init__(objects, **options)

    def _handle_object(self, obj):
        try:
            yield from super()._handle_object(obj)
        except (GeneratorExit, DeserializationError):
            raise
        except Exception as exc:
            raise DeserializationError(f"Error deserializing object: {exc}") from exc