From aad47fe1bfc789f4b0ad0dc6373c945663e91f93 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sun, 19 Apr 2026 01:49:19 -0400 Subject: Refs #36986 -- Replaced yaml's Serializer._value_from_field() time handling with a representer. --- django/core/serializers/pyyaml.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index 935d3cec49..02e56bdd52 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -23,17 +23,27 @@ except ImportError: 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_ordered_dict(self, data): return self.represent_mapping("tag:yaml.org,2002:map", data.items()) + 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( collections.OrderedDict, DjangoSafeDumper.represent_ordered_dict ) +DjangoSafeDumper.add_representer(datetime.time, DjangoSafeDumper.represent_time) class Serializer(PythonSerializer): @@ -41,19 +51,6 @@ class Serializer(PythonSerializer): internal_use_only = False - def _value_from_field(self, obj, field): - # A nasty special case: base YAML doesn't support serialization of time - # types (as opposed to dates or datetimes, which it does support). - # Since we want to use the "safe" serializer for better - # interoperability, we need to do something with those pesky times. - # Converting 'em to strings isn't perfect, but it's better than a - # "!!python/time" type which would halt deserialization under any other - # language. - value = super()._value_from_field(obj, field) - if isinstance(value, datetime.time): - value = str(value) - return value - def end_serialization(self): self.options.setdefault("allow_unicode", True) yaml.dump( -- cgit v1.3