1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
From: Luke Plant
Subject: Compatibility fix for JSON emitter with Django 1.5
Fixes compatibility with Django 1.5 to use json instead of
simplejson due to deprecation.
Origin: https://bitbucket.org/jespern/django-piston/pull-request/25/compatibility-fix-for-json-emitter-with
Bug-Ubuntu: https://launchpad.net/bugs/1184219
--- python-django-piston-0.2.3.orig/piston/emitters.py 2013-05-28 11:49:03.230954662 -0400
+++ python-django-piston-0.2.3/piston/emitters.py 2013-05-28 11:50:19.151073326 -0400
@@ -22,7 +22,6 @@
from django.db.models.query import QuerySet
from django.db.models import Model, permalink
-from django.utils import simplejson
from django.utils.xmlutils import SimplerXMLGenerator
from django.utils.encoding import smart_unicode
from django.core.urlresolvers import reverse, NoReverseMatch
@@ -30,6 +29,16 @@
from django.http import HttpResponse
from django.core import serializers
+import django
+if django.VERSION >= (1, 5):
+ # In 1.5 and later, DateTimeAwareJSONEncoder inherits from json.JSONEncoder,
+ # while in 1.4 and earlier it inherits from simplejson.JSONEncoder. The two
+ # are not compatible due to keyword argument namedtuple_as_object, and we
+ # have to ensure that the 'dumps' function we use is the right one.
+ import json
+else:
+ from django.utils import simplejson as json
+
from utils import HttpStatusCode, Mimer
from validate_jsonp import is_valid_jsonp_callback_value
@@ -388,7 +397,7 @@
"""
def render(self, request):
cb = request.GET.get('callback', None)
- seria = simplejson.dumps(self.construct(), cls=DateTimeAwareJSONEncoder, ensure_ascii=False, indent=4)
+ seria = json.dumps(self.construct(), cls=DateTimeAwareJSONEncoder, ensure_ascii=False, indent=4)
# Callback
if cb and is_valid_jsonp_callback_value(cb):
@@ -397,7 +406,7 @@
return seria
Emitter.register('json', JSONEmitter, 'application/json; charset=utf-8')
-Mimer.register(simplejson.loads, ('application/json',))
+Mimer.register(json.loads, ('application/json',))
class YAMLEmitter(Emitter):
"""
|