Django

Code

Changeset 3237

Show
Ignore:
Timestamp:
06/29/06 11:42:49 (2 years ago)
Author:
jacob
Message:

Added a JSON serializer, a few more tests, and a couple more lines of docs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/serializers/base.py

    r3225 r3237  
    3434            self.start_object(obj) 
    3535            for field in obj._meta.fields: 
    36                 if field.rel is None: 
     36                if field is obj._meta.pk: 
     37                    continue 
     38                elif field.rel is None: 
    3739                    self.handle_field(obj, field) 
    3840                else: 
  • django/trunk/django/core/serializers/__init__.py

    r3225 r3237  
    2121# Built-in serializers 
    2222BUILTIN_SERIALIZERS = { 
    23     "xml" : "django.core.serializers.xml_serializer", 
     23    "xml"    : "django.core.serializers.xml_serializer", 
     24    "python" : "django.core.serializers.python", 
     25    "json"   : "django.core.serializers.json", 
    2426} 
    2527 
  • django/trunk/django/core/serializers/xml_serializer.py

    r3225 r3237  
    33""" 
    44 
    5 from xml.dom import pulldom 
    6 from django.utils.xmlutils import SimplerXMLGenerator 
     5from django.conf import settings 
    76from django.core.serializers import base 
    87from django.db import models 
     8from django.utils.xmlutils import SimplerXMLGenerator 
     9from xml.dom import pulldom 
    910 
    1011class Serializer(base.Serializer): 
     
    1718        Start serialization -- open the XML document and the root element. 
    1819        """ 
    19         self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", "utf-8")) 
     20        self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET)) 
    2021        self.xml.startDocument() 
    2122        self.xml.startElement("django-objects", {"version" : "1.0"}) 
     
    5960        # serializer base class).  None is handled specially. 
    6061        value = self.get_string_value(obj, field) 
    61         if value is None: 
    62             self.xml.addQuickElement("None") 
    63         else: 
     62        if value is not None: 
    6463            self.xml.characters(str(value)) 
    6564 
     
    107106    def __init__(self, stream_or_string, **options): 
    108107        super(Deserializer, self).__init__(stream_or_string, **options) 
    109         self.encoding = self.options.get("encoding", "utf-8"
     108        self.encoding = self.options.get("encoding", settings.DEFAULT_CHARSET
    110109        self.event_stream = pulldom.parse(self.stream)  
    111110     
  • django/trunk/docs/serialization.txt

    r3225 r3237  
    7979--------------------- 
    8080 
    81 Django "ships" with a few included serializers, and there's a simple API for creating and registering your own... 
     81Django "ships" with a few included serializers: 
    8282 
    83 .. note:: 
     83    ==========  ============================================================== 
     84    Identifier  Information 
     85    ==========  ============================================================== 
     86    ``xml``     Serializes to and from a simple XML dialect. 
    8487 
    85     ... which will be documented once the API is stable :) 
     88    ``json``    Serializes to and from JSON_ (using a version of simplejson_ 
     89                bundled with Django). 
     90 
     91    ``python``  Translates to and from "simple" Python objects (lists, dicts, 
     92                strings, etc.).  Not really all that useful on its own, but  
     93                used as a base for other serializers. 
     94    ==========  ============================================================== 
     95 
     96.. _json: http://json.org/ 
     97.. _simplejson: http://undefined.org/python/#simplejson 
     98 
     99Writing custom serializers 
     100`````````````````````````` 
     101 
     102XXX ... 
  • django/trunk/tests/modeltests/serializers/models.py

    r3225 r3237  
    9292[<Article: Poker has no place on television>, <Article: Time to reform copyright>] 
    9393 
     94# Django also ships with a built-in JSON serializers 
     95>>> json = serializers.serialize("json", Category.objects.filter(pk=2)) 
     96>>> json 
     97'[{"pk": "2", "model": "serializers.category", "fields": {"name": "Music"}}]' 
     98 
     99# You can easily create new objects by deserializing data with an empty PK 
     100# (It's easier to demo this with JSON...) 
     101>>> new_author_json = '[{"pk": null, "model": "serializers.author", "fields": {"name": "Bill"}}]' 
     102>>> for obj in serializers.deserialize("json", new_author_json): 
     103...     obj.save() 
     104>>> Author.objects.all() 
     105[<Author: Bill>, <Author: Jane>, <Author: Joe>] 
     106 
     107# All the serializers work the same 
     108>>> json = serializers.serialize("json", Article.objects.all()) 
     109>>> for obj in serializers.deserialize("json", json): 
     110...     print obj 
     111<DeserializedObject: Poker has no place on television> 
     112<DeserializedObject: Time to reform copyright> 
     113 
     114>>> json = json.replace("Poker has no place on television", "Just kidding; I love TV poker") 
     115>>> for obj in serializers.deserialize("json", json): 
     116...     obj.save() 
     117 
     118>>> Article.objects.all() 
     119[<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>] 
     120 
    94121"""