Hi All,
I'm running into a situation that completely baffles me. I am
writing unit tests for my Django view that accepts JSON and returns
JSON back to the client. Right now, the client is the Django unit
test client. What I've done is in my test, I create a Django model
object, let's call it Person for now. I serialize that to JSON and
then use
client.post('VIEWURL', {'data': person_as_json}). Whenever I
retrieve the data, using request.POST['data'], I get a string that
Django says cannot be deserialized. (No JSON object could be
decoded) If I deserialize the exact same string in my test case, it
deserializes just fine. In fact, if I use simplejson from
django.utils to deserialize the JSON string in request.POST['data'],
simplejson has no problem deserializing it. Now before I ask any
specific questions, let me give you some background on my needs.
I am writing a Django-based server for a management application.
(This application will be managing objects via forms and such.) My
plan is to have a a client that is not Python based which is where
JSON comes in since JSON parser implementations are available in many
languages. Now, whenever I want to create a new Person object on the
server, my client creates a Person representation, using form fields/
values, and submits to the server.
Well, it sounds pretty simple until Django tries to deserialize.
Since I'm writing the client, there is no reason I couldn't create a
Django-specific JSON encoder on my client so that Django could
deserialize but even that appears to be failing since a JSON string
deserializes fine in my test but not when retrieved from
request.POST['data']. So here are my questions:
1) Does anyone know of a detailed example of doing this? I've googled
but not found anything that helps me get past this.
2) Anyone know why a string would be Django-deserializable in a unit
test but not on the server? I may be missing a step during the
retrieval from request.POST but I've not found any reference to this
missing step(s).
3) Is this a viable approach? Can I create a Django-specific JSON
encoder on my client and deserialize on the server or should I use
another approach like submitting form contents as key/value pairs and
then iterating over them on the server to create my object needing to
be saved.
I can give you code if you'd like but for brevity, here are examples
of what I'm doing in my unit tests and on the server:
Unit test code
---------------------
.....
rc = create_repository_container()
data = serializers.serialize("json", [rc], ensure_ascii=False)
print "\n" + data
.....
Unit test output
----------------------
[{"pk": null, "model": "svn.repositorycontainer", "fields":
{"display_name": "Repository Container 1", "description": "Repository
Container 1 description", "server_type": "Apache", "created": null,
"authz_path": "/Users/jwhitlock/dev/raven/server/raven_server/../
raven_server/svn/data/1/container_1.authz", "active": true, "path": "/
Users/jwhitlock/dev/raven/server/raven_server/../raven_server/svn/data/
1"}}]
Server-side code
-------------------------
.....
json_data = serializers.deserialize("json", request.POST['data'])
print "\n" + str(sjson_data)
.....
Server-side output
---------------------------
[{"pk": null, "model": "svn.repositorycontainer", "fields":
{"display_name": "Repository Container 1", "description": "Repository
Container 1 description", "server_type": "Apache", "created": null,
"authz_path": "/Users/jwhitlock/dev/raven/server/raven_server/../
raven_server/svn/data/1/container_1.authz", "active": true, "path": "/
Users/jwhitlock/dev/raven/server/raven_server/../raven_server/svn/data/
1"}}]
From what I can tell, the strings are the same yet the unit tests
successfully deserializes while the server-side creates the following
error: No JSON object could be decoded
Any help would be appreciated.
Take care,
Jeremy