Matthew Schinckel  committed 42f6005

Better fix for deserialized objects having unicode keys.

  • Participants
  • Parent commits e715175
  • Branches default

Comments (0)

Files changed (3)

File rest_api/

                 # Not sure how m2m will go here.
                 if incoming.get(f.get_accessor_name(), False):
                     related[f.get_accessor_name()] = incoming.pop(f.get_accessor_name())
-            instance = self.model(**dict([(str(k),v) for (k,v) in incoming.iteritems()]))
+            instance = self.model(**incoming)
                 # If there are audit fields, then update the values.
                 instance.created_by = request.user
                             if f.rel:
                                 val[k] =
-                        getattr(instance, key).create(**dict([(str(_k), _v) for _k, _v in val.items()]))
+                        getattr(instance, key).create(**val)
                 elif getattr(instance, key) != value:
                         setattr(instance, key, value)

File rest_api/

 It uses a subclass of DjangoJSONEncoder, however.
-from django.core.serializers.json import simplejson, DjangoJSONEncoder
+from django.core.serializers.json import simplejson, DjangoJSONEncoder#, JSONDecoder
 from django.core.exceptions import FieldError, ObjectDoesNotExist
 from django.db.models import FieldDoesNotExist
 from django.db import models
 def deserialize(stream):
-    # We should be able to just use the straight loads, as django ORM will
-    # handle datetime as a string, and Decimal.
-    return simplejson.loads(stream, parse_float=Decimal)
+    """
+    We need to ensure any dictionary keys are strings, not unicode strings.
+    This appears to only happen with some versions of simplejson.
+    We also want to use Decimal instead of floats, just about everywhere.
+    """
+    return simplejson.loads(stream, 
+        parse_float=Decimal,
+        object_hook=lambda dictionary: dict([(str(k),v) for k,v in dictionary.items()]))

File rest_api/

 from rest_api import http
 from rest_api.options import ModelApi
+from rest_api import serializers
 from django import template
 from django.core.exceptions import ImproperlyConfigured
 from django.utils.translation import ugettext as _
 from django.utils.text import capfirst
 from django.utils.safestring import mark_safe
-from django.core.serializers.json import simplejson
+# from django.core.serializers.json import simplejson
 from django.core.exceptions import ObjectDoesNotExist
 from django.conf.urls.defaults import patterns, url, include
                 # TODO: use which ever serializer we need to use here,
                 # rather than just JSON
                 if request.raw_post_data:
-           = simplejson.loads(request.raw_post_data)
+           = serializers.deserialize(request.raw_post_data)
                 # We now call the actual view. Because it may or may not
                 # return an HttpResponse, we can wrap the response in one
                 # if it returns something else (probably a queryset).