title: JSON Encoding with Django adding extra \\ characters
body: <p>I'm trying to create a function that will convert a dictionary containing a message and a Django model instance into JSON, that I can pass back to the client.  For example, I have the model Test defined in models.py.</p>
  
  <pre><code>from django.db import models
  
  class Test(models.Model):
      test_field = models.CharField(max_length=40)
  </code></pre>
  
  <p>I've defined this extension of the simplejson JSONEncoder based on the this <a href="https://stackoverflow.com/questions/2249792/json-serializing-django-models-with-simplejson">stackoverflow question</a>:</p>
  
  <pre><code>from django.core.serializers import serialize
  from django.utils.simplejson import dumps, loads, JSONEncoder
  from django.db.models.query import QuerySet
  from django.db import models
  from django.utils.functional import curry
  
  class DjangoJSONEncoder(JSONEncoder):
      def default(self, obj):
          if isinstance(obj, QuerySet):
              # `default` must return a python serializable
              # structure, the easiest way is to load the JSON
              # string produced by `serialize` and return it
              return loads(serialize('json', obj))
          if isinstance(obj, models.Model):
              #do the same as above by making it a queryset first
              set_obj = [obj]
              set_str = serialize('json', set_obj)
              #eliminate brackets in the beginning and the end 
              str_obj = set_str[1:len(set_str)-2]
              return str_obj
          return JSONEncoder.default(self,obj)
  
  # partial function, we can now use dumps(my_dict) instead
  # of dumps(my_dict, cls=DjangoJSONEncoder)
  dumps = curry(dumps, cls=DjangoJSONEncoder)
  </code></pre>
  
  <p>Then I go about creating an instance of this along with a status message:</p>
  
  <pre><code>t = Test(test_field="hello")
  d = {"entry": t, "message": "Congratulations"}
  json = dumps(d)
  </code></pre>
  
  <p>The contents of json are:</p>
  
  <pre><code>{"entry": "{\\"pk\\": null, \\"model\\": \\"hours.test\\", \\"fields\\": {\\"test_field\\": \\"hello\\"}", "message": "Congratulations"}
  </code></pre>
  
  <p>Which is basically what I want except for all the extra <code>\\</code> characters.  Why are these being inserted into the json?  How can I modify my DjangoJSONEncoder so it doesn't insert the \ characters?</p>
  
  <p><strong>NOTE</strong></p>
  
  <p>If I just encode the model instance manually I don't get all the extra <code>\\</code> characters.</p>
  
  <pre><code>s = serialize('json', [t])
  s[1:len(s)-2]
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>{"pk": null, "model": "hours.test", "fields": {"test_field": "hello"}
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on the advice of Daniel Roseman and Leopd I modified the DjangoJSONEncoder class to the following:</p>
  
  <pre><code>class DjangoJSONEncoder(JSONEncoder):
      def default(self, obj):
          if isinstance(obj, QuerySet):
              # `default` must return a python serializable
              # structure, the easiest way is to load the JSON
              # string produced by `serialize` and return it
              return loads(serialize('python', obj))
          if isinstance(obj, models.Model):
              #do the same as above by making it a list first
              return serialize('python', [obj])[0]
          return JSONEncoder.default(self,obj)
  </code></pre>