Created
August 1, 2023 02:27
-
-
Save Arathi/1a6b3a9676f067e2236f31a1a7d213c1 to your computer and use it in GitHub Desktop.
JSON-RPC报文结构
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonInclude | |
open class Request<P>( | |
/** | |
* A String specifying the version of the JSON-RPC protocol. | |
* MUST be exactly "2.0". | |
*/ | |
val jsonrpc: String = Version20, | |
/** | |
* A String containing the name of the method to be invoked. | |
* Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved | |
* for rpc-internal methods and extensions and MUST NOT be used for anything else. | |
*/ | |
val method: String, | |
/** | |
* A Structured value that holds the parameter values to be used during the invocation of the method. | |
* This member MAY be omitted. | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val params: P?, | |
/** | |
* An identifier established by the Client that MUST contain a String, Number, or NULL value if included. | |
* If it is not included it is assumed to be a notification. | |
* The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val id: Int? = null, | |
) { | |
companion object { | |
const val Version20 = "2.0" | |
} | |
} | |
open class Response<R, D>( | |
/** | |
* A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0". | |
*/ | |
val jsonrpc: String = Request.Version20, | |
/** | |
* This member is REQUIRED on success. | |
* | |
* This member MUST NOT exist if there was an error invoking the method. | |
* | |
* The value of this member is determined by the method invoked on the Server. | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val result: R? = null, | |
/** | |
* This member is REQUIRED on error. | |
* | |
* This member MUST NOT exist if there was no error triggered during invocation. | |
* | |
* The value for this member MUST be an Object as defined in section 5.1. | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val error: Error<D>? = null, | |
/** | |
* This member is REQUIRED. | |
* | |
* It MUST be the same as the value of the id member in the Request Object. | |
* | |
* If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null. | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val id: Int? = null, | |
) { | |
companion object { | |
fun <R> result(id: Int?, result: R): Response<R, Void> { | |
return Response( | |
jsonrpc = Request.Version20, | |
result = result, | |
error = null, | |
id = id | |
) | |
} | |
fun <D> error(id: Int?, code: Int, message: String = "", data: D? = null): Response<Void, D> { | |
return Response( | |
jsonrpc = Request.Version20, | |
result = null, | |
error = Error(code, message, data), | |
id = id | |
) | |
} | |
} | |
} | |
class Error<D>( | |
/** | |
* A Number that indicates the error type that occurred. | |
* | |
* This MUST be an integer. | |
*/ | |
val code: Int, | |
/** | |
* A String providing a short description of the error. | |
* | |
* The message SHOULD be limited to a concise single sentence. | |
*/ | |
var message: String = "", | |
/** | |
* A Primitive or Structured value that contains additional information about the error. | |
* This may be omitted. | |
* The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.). | |
*/ | |
@get:JsonInclude(JsonInclude.Include.NON_NULL) | |
val data: D? = null, | |
) { | |
init { | |
if (message == "") { | |
message = when(code) { | |
0 -> "成功" | |
ParseError -> "解析错误" | |
InvalidRequest -> "非法请求" | |
MethodNotFound -> "方法不存在" | |
InvalidParams -> "非法参数" | |
InternalError -> "内部错误" | |
else -> "未知错误" | |
} | |
} | |
} | |
companion object { | |
const val ParseError = -32700 | |
const val InvalidRequest = -32600 | |
const val MethodNotFound = -32601 | |
const val InvalidParams = -32602 | |
const val InternalError = -32603 | |
// -32000 to -32099 ServerError | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment