def write(self, chunk):
        """Writes the given chunk to the output buffer.

        To write the output to the network, use the flush() method below.

        If the given chunk is a dictionary, we write it as JSON and set
        the Content-Type of the response to be text/javascript.
        """
        assert not self._finished
        if getattr(self, 'content_type', None) == 'application/json':
            chunk = escape.json_encode(chunk)
            #self.set_content_type("text/javascript; charset=UTF-8")
            self.set_content_type(self.content_type)
        
        elif hasattr(self, 'content_type'):
            self.set_content_type(self.content_type)
            
        chunk = escape.utf8(chunk)
        self._write_buffer.append(chunk)
 def wrap(sock, *args, **kwargs):
     results = cb(*args, **kwargs)
     sock.send(escape.json_encode(results) + "\r\n\r\n")
     sock.close()