Added a YAML serializer built on PyYAML (http://pyyaml.org/). This serializer will only be enabled if PyYAML is installed, so I've not written any unit tests until I figure out how to make them work similarly. Still, the serializer is just a thin layer over the base Python serializer, and seems to Just Work.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4712 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
5295f54b3c
commit
9f0c545add
|
@ -25,6 +25,13 @@ BUILTIN_SERIALIZERS = {
|
||||||
"json" : "django.core.serializers.json",
|
"json" : "django.core.serializers.json",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Check for PyYaml and register the serializer if it's available.
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
_serializers = {}
|
_serializers = {}
|
||||||
|
|
||||||
def register_serializer(format, serializer_module):
|
def register_serializer(format, serializer_module):
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
"""
|
||||||
|
YAML serializer.
|
||||||
|
|
||||||
|
Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
from django.core.serializers.python import Serializer as PythonSerializer
|
||||||
|
from django.core.serializers.python import Deserializer as PythonDeserializer
|
||||||
|
try:
|
||||||
|
from cStringIO import StringIO
|
||||||
|
except ImportError:
|
||||||
|
from StringIO import StringIO
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
class Serializer(PythonSerializer):
|
||||||
|
"""
|
||||||
|
Convert a queryset to JSON.
|
||||||
|
"""
|
||||||
|
def end_serialization(self):
|
||||||
|
yaml.dump(self.objects, self.stream, **self.options)
|
||||||
|
|
||||||
|
def getvalue(self):
|
||||||
|
return self.stream.getvalue()
|
||||||
|
|
||||||
|
def Deserializer(stream_or_string, **options):
|
||||||
|
"""
|
||||||
|
Deserialize a stream or string of JSON data.
|
||||||
|
"""
|
||||||
|
if isinstance(stream_or_string, basestring):
|
||||||
|
stream = StringIO(stream_or_string)
|
||||||
|
else:
|
||||||
|
stream = stream_or_string
|
||||||
|
for obj in PythonDeserializer(yaml.load(stream)):
|
||||||
|
yield obj
|
||||||
|
|
Loading…
Reference in New Issue