예제 #1
0
    def from_json(json):
        if type(json) != dict:
            return "message is not a JSON object"

        # Make a copy to avoid modifying the caller's dict.
        json = dict(json)

        if "method" in json:
            method = json.pop("method")
            if type(method) not in [str, unicode]:
                return "method is not a JSON string"
        else:
            method = None

        params = json.pop("params", None)
        result = json.pop("result", None)
        error = json.pop("error", None)
        id_ = json.pop("id", None)
        if len(json):
            return "message has unexpected member \"%s\"" % json.popitem()[0]

        if result is not None:
            msg_type = Message.T_REPLY
        elif error is not None:
            msg_type = Message.T_ERROR
        elif id_ is not None:
            msg_type = Message.T_REQUEST
        else:
            msg_type = Message.T_NOTIFY

        msg = Message(msg_type, method, params, result, error, id_)
        validation_error = msg.is_valid()
        if validation_error is not None:
            return validation_error
        else:
            return msg
예제 #2
0
파일: jsonrpc.py 프로젝트: M3S/ovs-rstp
    def from_json(json):
        if not isinstance(json, dict):
            return "message is not a JSON object"

        # Make a copy to avoid modifying the caller's dict.
        json = dict(json)

        if "method" in json:
            method = json.pop("method")
            if not isinstance(method, six.string_types):
                return "method is not a JSON string"
        else:
            method = None

        params = json.pop("params", None)
        result = json.pop("result", None)
        error = json.pop("error", None)
        id_ = json.pop("id", None)
        if len(json):
            return "message has unexpected member \"%s\"" % json.popitem()[0]

        if result is not None:
            msg_type = Message.T_REPLY
        elif error is not None:
            msg_type = Message.T_ERROR
        elif id_ is not None:
            msg_type = Message.T_REQUEST
        else:
            msg_type = Message.T_NOTIFY

        msg = Message(msg_type, method, params, result, error, id_)
        validation_error = msg.is_valid()
        if validation_error is not None:
            return validation_error
        else:
            return msg