# Buggy code
def _exec_jsonrpc(self, name, *args, **kwargs):
    req = request_builder(name, *args, **kwargs)
    reqid = req["id"]

    if not os.path.exists(self.socket_path):
        raise ConnectionError(
            "socket_path does not exist or cannot be found."
            "\nSee the socket_path issue catergory in Network Debug and Troubleshooting Guide"
        )

    try:
        data = json.dumps(req)
    except TypeError as exc:
        data = req.get("params")

        if isinstance(data, dict):
            data = data.get("var_options", {})

            for key, value in iteritems(data):
                try:
                    dummy = json.dumps(value)
                except TypeError:
                    raise ConnectionError(
                        "Failed to encode some variables as JSON for communication with ansible-connection. "
                        "Please open an issue and mention that the culprit is most likely '%s'"
                        % key
                    )

        raise ConnectionError(
            "Failed to encode some variables as JSON for communication with ansible-connection. "
            "The original exception was: %s" % to_text(exc)
        )

    try:
        out = self.send(data)
    except socket.error as e:
        raise ConnectionError(
            "unable to connect to socket. See the socket_path issue catergory in Network Debug and Troubleshooting Guide",
            err=to_text(e, errors="surrogate_then_replace"),
            exception=traceback.format_exc(),
        )

    try:
        response = json.loads(out)
    except ValueError:
        params = list(args) + ["{0}={1}".format(k, v) for k, v in iteritems(kwargs)]
        params = ", ".join(params)
        raise ConnectionError(
            "Unable to decode JSON from response to {0}({1}). Received '{2}'.".format(
                name, params, out
            )
        )

    if response["id"] != reqid:
        raise ConnectionError("invalid json-rpc id received")

    return response


# Traceback (TypeError)
fatal: [redacted]: FAILED! => {"msg": "Traceback (most recent call last):
File \"/usr/bin/ansible-connection\", line 321, in <module>
main()
File \"/usr/bin/ansible-connection\", line 287, in main
conn.set_options(var_options=variables)
File \"/usr/lib/python2.7/site-packages/ansible/module_utils/connection.py\", line 167, in __rpc__
response = self._exec_jsonrpc(name, *args, **kwargs)
File \"/usr/lib/python2.7/site-packages/ansible/module_utils/connection.py\", line 144, in _exec_jsonrpc
data = json.dumps(req)
File \"/usr/lib/python2.7/json/__init__.py\", line 244, in dumps
return _default_encoder.encode(obj)
File \"/usr/lib/python2.7/json/encoder.py\", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File \"/usr/lib/python2.7/json/encoder.py\", line 270, in iterencode
return _iterencode(o, 0)
File \"/usr/lib/python2.7/json/encoder.py\", line 184, in default
raise TypeError(repr(o) + \" is not JSON serializable\")
TypeError: u'***redacted***' is not JSON serializable
"}

# Fixed code
def _exec_jsonrpc(self, name, *args, **kwargs):
    req = request_builder(name, *args, **kwargs)
    reqid = req["id"]

    if not os.path.exists(self.socket_path):
        raise ConnectionError(
            "socket_path does not exist or cannot be found."
            "\nSee the socket_path issue catergory in Network Debug and Troubleshooting Guide"
        )

    try:
        data = json.dumps(req, cls=AnsibleJSONEncoder)
    except TypeError as exc:
        raise ConnectionError(
            "Failed to encode some variables as JSON for communication with ansible-connection. "
            "The original exception was: %s" % to_text(exc)
        )

    try:
        out = self.send(data)
    except socket.error as e:
        raise ConnectionError(
            "unable to connect to socket. See the socket_path issue catergory in Network Debug and Troubleshooting Guide",
            err=to_text(e, errors="surrogate_then_replace"),
            exception=traceback.format_exc(),
        )

    try:
        response = json.loads(out)
    except ValueError:
        params = list(args) + ["{0}={1}".format(k, v) for k, v in iteritems(kwargs)]
        params = ", ".join(params)
        raise ConnectionError(
            "Unable to decode JSON from response to {0}({1}). Received '{2}'.".format(
                name, params, out
            )
        )

    if response["id"] != reqid:
        raise ConnectionError("invalid json-rpc id received")

    return response