Permalink
Browse files

Update the serializer to return an http response and optionall, retur…

…n a value as a dict.
  • Loading branch information...
1 parent 85ee823 commit 4b62183d5c5bc9784a77e446c7feaaff0bd24540 @buchuki committed May 12, 2011
Showing with 87 additions and 3 deletions.
  1. +6 −1 ChangeLog
  2. +37 −0 README
  3. +1 −1 djason/__init__.py
  4. +8 −1 djason/json.py
  5. +35 −0 djason/json_dict.py
View
7 ChangeLog
@@ -1,4 +1,9 @@
-Version 0.0.1
+Version 0.2
+- Added a serializer that allows including the serialized object inside an
+object with extra attributes
+- Added an 'httpresponse' option that will automatically wrap the serialized
+stream in an HttpResponse object
+Version 0.1
- First public released
- forked from https://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers
View
37 README
@@ -29,7 +29,44 @@ frontend, rather than intended as a serializer to a file. Here's an example:
}
]
+The default serializer (djason.json) spits out a list of objects as a string.
+You can optionally have it return that list wrapped in an HttpResponse object
+by passing the httpresponse=True option into serialize.
+There is a second serializer that takes two optional parameters, attributes (a
+dict) and list_name (a string). This serializer returns a dictionary with any
+key value attributes in the attributes dictionary, and one extra attribute
+named list_name, which contains the list of objects as returned by the json
+serializer. For example:
+
+>>> print s.serialize(User.objects.all(), relations=['groups'], excludes=['user_permissions'], indent=4, attributes={'somekey': 'somevalue'}, list_name="users")
+{
+ "somekey": "somevalue",
+ "users": [
+ {
+ "username": "dusty",
+ "first_name": "",
+ "last_name": "",
+ "is_active": true,
+ "is_superuser": true,
+ "is_staff": true,
+ "last_login": "2010-11-26 13:27:07",
+ "groups": [
+ {
+ "pk": 1,
+ "model": "auth.group",
+ "name": "hello",
+ "permissions": []
+ }
+ ],
+ "pk": 1,
+ "model": "auth.user",
+ "password": "sha1$8f816$b4c8139100c314b46f1ca931858e4d6d5c61917f",
+ "email": "dusty@linux.ca",
+ "date_joined": "2010-11-22 17:55:40"
+ }
+ ]
+}
This project was originally forked from:
https://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers
View
2 djason/__init__.py
@@ -1 +1 @@
-__version__ = '0.1'
+__version__ = '0.2'
View
9 djason/json.py
@@ -5,6 +5,7 @@
from python import Serializer as PythonSerializer
from django.core.serializers.json import Deserializer as JSONDeserializer, \
DjangoJSONEncoder
+from django.http import HttpResponse
class Serializer(PythonSerializer):
"""
@@ -18,6 +19,7 @@ def end_serialization(self):
self.options.pop('relations', None)
self.options.pop('extras', None)
self.options.pop('use_natural_keys', None)
+ self.use_httpresponse=self.options.pop('httpresponse', None)
simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder,
**self.options)
@@ -28,6 +30,11 @@ def getvalue(self):
"""
if callable(getattr(self.stream, 'getvalue', None)):
- return self.stream.getvalue()
+ value = self.stream.getvalue()
+
+ if self.use_httpresponse:
+ return HttpResponse(value)
+ else:
+ return value
Deserializer = JSONDeserializer
View
35 djason/json_dict.py
@@ -0,0 +1,35 @@
+"""
+Serialize data to/from JSON storing the results in an object with
+possible extra attributes.
+
+So where the djason.json serializer would return
+[{attr1: val1},...] this one will return
+{akey: avalue, anotherkey:anothervalue, objects:[{attr1:val1},...]}
+"""
+from django.utils import simplejson
+from json import Serializer as JsonSerializer
+from django.core.serializers.json import Deserializer as JSONDeserializer, \
+ DjangoJSONEncoder
+
+class Serializer(JsonSerializer):
+ """
+ Convert a queryset to JSON.
+ """
+ def end_serialization(self):
+ """Output a JSON encoded queryset."""
+ self.options.pop('stream', None)
+ self.options.pop('fields', None)
+ self.options.pop('excludes', None)
+ self.options.pop('relations', None)
+ self.options.pop('extras', None)
+ self.options.pop('use_natural_keys', None)
+ self.use_httpresponse=self.options.pop('httpresponse', None)
+
+ attributes = self.options.pop('attributes', {})
+ list_name = self.options.pop('list_name', 'objects')
+ attributes[list_name] = self.objects
+ simplejson.dump(attributes, self.stream, cls=DjangoJSONEncoder,
+ **self.options)
+
+
+Deserializer = JSONDeserializer

0 comments on commit 4b62183

Please sign in to comment.