Django

Code

Ticket #5268: patch_yaml_safe_dumper.txt

File patch_yaml_safe_dumper.txt, 1.6 kB (added by django@poelzi.org, 1 year ago)

add safe_dump argument to yaml serializer

Line 
1 Index: django/core/serializers/pyyaml.py
2 ===================================================================
3 --- django/core/serializers/pyyaml.py   (revision 6021)
4 +++ django/core/serializers/pyyaml.py   (working copy)
5 @@ -20,8 +20,14 @@
6      def end_serialization(self):
7          self.options.pop('stream', None)
8          self.options.pop('fields', None)
9 -        yaml.dump(self.objects, self.stream, **self.options)
10 +        if self.options.has_key("safe_dump") and \
11 +           self.options.pop("safe_dump") == True:
12 +            yaml.safe_dump(self.objects, self.stream, **self.options)
13 +        else:
14 +            self.options.pop('safe_dump', None)
15 +            yaml.dump(self.objects, self.stream, **self.options)
16  
17 +
18      def getvalue(self):
19          return self.stream.getvalue()
20  
21 Index: docs/serialization.txt
22 ===================================================================
23 --- docs/serialization.txt      (revision 6021)
24 +++ docs/serialization.txt      (working copy)
25 @@ -135,6 +135,21 @@
26      json_serializer = serializers.get_serializer("json")()
27      json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
28  
29 +yaml
30 +~~~~
31 +
32 +If want dump complex data that will be used by non python clients you
33 +want to use ``safe_dump=True`` as a parameter to the ``serialize()``
34 +call. Otherwise UTF-8 strings will end up as type "!!python/unicode".
35 +Using a large width solves problems with some parsers, too.
36 +
37 +For example::
38 +
39 +    yaml_serializer = serializers.get_serializer("json")()
40 +    yaml_serializer.serialize(queryset, safe_dump=True,
41 +stream=response, width=9999999999)
42 +
43 +
44  Writing custom serializers
45  ``````````````````````````
46