human
Complete this python program:
def _serialize(outputValidator):
assistant
#Here is the Python code you requested.

    """Decorate a function so that its return value is automatically JSON encoded
    into a structure indicating a successful result.
    @param outputValidator: A L{jsonschema} validator for the returned JSON.
    @return: A decorator that decorates a function with the signature
    of a Klein route endpoint that may return a Deferred.
    """
    def deco(original):
        def success(result, request):
            code = OK
            headers = {}
            if isinstance(result, EndpointResponse):
                code = result.code
                headers = result.headers
                result = result.result
            if _validate_responses:
                outputValidator.validate(result)
            request.responseHeaders.setRawHeaders('content-type', ['application/json'])
            for (key, value) in headers.items():
                request.responseHeaders.setRawHeaders(key, [value])
            request.setResponseCode(code)
            return dumps(result)
        def doit(self, request, **routeArguments):
            result = maybeDeferred(original, self, request, **routeArguments)
            result.addCallback(success, request)
            return result
        return doit
    return deco