def _cast(request, output):
    if hasattr(output, "to_html"):
        output = output.to_html()

    if isinstance(output, (tuple, list, dict)):
        with request._context as app:
            return json.dumps(output, cls=app.json_encoder)

    elif isinstance(output, unicode):
        return output.encode(request._charset)

    elif not isinstance(output, str):
        with request._context:
            return str(output)

    return output
Convert an output object into something we can send over a connection.
dataset/ETHPy150Open ecdavis/pants/pants/web/asynchronous.py/_cast