'Renders the given data as json.
If callback is valid, renders data as jsonp'

def json(self, data):

    callback = self.request.get('callback', default_value=False)
    data = json.dumps(data)
    if callback:
        self.response.headers['Content-Type'] = 'application/javascript'
        data = (((callback + '(') + data) + ');')
    else:
        self.response.headers['Content-Type'] = 'application/json'
    self.response.out.write(data)
