| 1 |
""" |
|---|
| 2 |
Interfaces for serializing Django objects. |
|---|
| 3 |
|
|---|
| 4 |
Usage:: |
|---|
| 5 |
|
|---|
| 6 |
from django.core import serializers |
|---|
| 7 |
json = serializers.serialize("json", some_query_set) |
|---|
| 8 |
objects = list(serializers.deserialize("json", json)) |
|---|
| 9 |
|
|---|
| 10 |
To add your own serializers, use the SERIALIZATION_MODULES setting:: |
|---|
| 11 |
|
|---|
| 12 |
SERIALIZATION_MODULES = { |
|---|
| 13 |
"csv" : "path.to.csv.serializer", |
|---|
| 14 |
"txt" : "path.to.txt.serializer", |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
""" |
|---|
| 18 |
|
|---|
| 19 |
from django.conf import settings |
|---|
| 20 |
|
|---|
| 21 |
# Built-in serializers |
|---|
| 22 |
BUILTIN_SERIALIZERS = { |
|---|
| 23 |
"xml" : "django.core.serializers.xml_serializer", |
|---|
| 24 |
"python" : "django.core.serializers.python", |
|---|
| 25 |
"json" : "django.core.serializers.json", |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
# Check for PyYaml and register the serializer if it's available. |
|---|
| 29 |
try: |
|---|
| 30 |
import yaml |
|---|
| 31 |
BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml" |
|---|
| 32 |
except ImportError: |
|---|
| 33 |
pass |
|---|
| 34 |
|
|---|
| 35 |
_serializers = {} |
|---|
| 36 |
|
|---|
| 37 |
def register_serializer(format, serializer_module, serializers=None): |
|---|
| 38 |
""""Register a new serializer. |
|---|
| 39 |
|
|---|
| 40 |
``serializer_module`` should be the fully qualified module name |
|---|
| 41 |
for the serializer. |
|---|
| 42 |
|
|---|
| 43 |
If ``serializers`` is provided, the registration will be added |
|---|
| 44 |
to the provided dictionary. |
|---|
| 45 |
|
|---|
| 46 |
If ``serializers`` is not provided, the registration will be made |
|---|
| 47 |
directly into the global register of serializers. Adding serializers |
|---|
| 48 |
directly is not a thread-safe operation. |
|---|
| 49 |
""" |
|---|
| 50 |
module = __import__(serializer_module, {}, {}, ['']) |
|---|
| 51 |
if serializers is None: |
|---|
| 52 |
_serializers[format] = module |
|---|
| 53 |
else: |
|---|
| 54 |
serializers[format] = module |
|---|
| 55 |
|
|---|
| 56 |
def unregister_serializer(format): |
|---|
| 57 |
"Unregister a given serializer. This is not a thread-safe operation." |
|---|
| 58 |
del _serializers[format] |
|---|
| 59 |
|
|---|
| 60 |
def get_serializer(format): |
|---|
| 61 |
if not _serializers: |
|---|
| 62 |
_load_serializers() |
|---|
| 63 |
return _serializers[format].Serializer |
|---|
| 64 |
|
|---|
| 65 |
def get_serializer_formats(): |
|---|
| 66 |
if not _serializers: |
|---|
| 67 |
_load_serializers() |
|---|
| 68 |
return _serializers.keys() |
|---|
| 69 |
|
|---|
| 70 |
def get_public_serializer_formats(): |
|---|
| 71 |
if not _serializers: |
|---|
| 72 |
_load_serializers() |
|---|
| 73 |
return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only] |
|---|
| 74 |
|
|---|
| 75 |
def get_deserializer(format): |
|---|
| 76 |
if not _serializers: |
|---|
| 77 |
_load_serializers() |
|---|
| 78 |
return _serializers[format].Deserializer |
|---|
| 79 |
|
|---|
| 80 |
def serialize(format, queryset, **options): |
|---|
| 81 |
""" |
|---|
| 82 |
Serialize a queryset (or any iterator that returns database objects) using |
|---|
| 83 |
a certain serializer. |
|---|
| 84 |
""" |
|---|
| 85 |
s = get_serializer(format)() |
|---|
| 86 |
s.serialize(queryset, **options) |
|---|
| 87 |
return s.getvalue() |
|---|
| 88 |
|
|---|
| 89 |
def deserialize(format, stream_or_string): |
|---|
| 90 |
""" |
|---|
| 91 |
Deserialize a stream or a string. Returns an iterator that yields ``(obj, |
|---|
| 92 |
m2m_relation_dict)``, where ``obj`` is a instantiated -- but *unsaved* -- |
|---|
| 93 |
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name : |
|---|
| 94 |
list_of_related_objects}``. |
|---|
| 95 |
""" |
|---|
| 96 |
d = get_deserializer(format) |
|---|
| 97 |
return d(stream_or_string) |
|---|
| 98 |
|
|---|
| 99 |
def _load_serializers(): |
|---|
| 100 |
""" |
|---|
| 101 |
Register built-in and settings-defined serializers. This is done lazily so |
|---|
| 102 |
that user code has a chance to (e.g.) set up custom settings without |
|---|
| 103 |
needing to be careful of import order. |
|---|
| 104 |
""" |
|---|
| 105 |
global _serializers |
|---|
| 106 |
serializers = {} |
|---|
| 107 |
for format in BUILTIN_SERIALIZERS: |
|---|
| 108 |
register_serializer(format, BUILTIN_SERIALIZERS[format], serializers) |
|---|
| 109 |
if hasattr(settings, "SERIALIZATION_MODULES"): |
|---|
| 110 |
for format in settings.SERIALIZATION_MODULES: |
|---|
| 111 |
register_serializer(format, settings.SERIALIZATION_MODULES[format], serializers) |
|---|
| 112 |
_serializers = serializers |
|---|