{"id":24959424,"url":"https://github.com/lamgc/jsonrpc-java","last_synced_at":"2025-03-28T21:15:37.965Z","repository":{"id":106895964,"uuid":"608063578","full_name":"LamGC/jsonrpc-java","owner":"LamGC","description":"Simple and flexible Json RPC library.","archived":false,"fork":false,"pushed_at":"2023-10-19T05:20:50.000Z","size":118,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-03T07:47:05.070Z","etag":null,"topics":["json-rpc","jsonrpc","jsonrpc-lib","rpc-library"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LamGC.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-01T08:42:48.000Z","updated_at":"2023-07-27T05:49:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"6025fcbc-fd8e-4ba6-af83-ed3176b8fedc","html_url":"https://github.com/LamGC/jsonrpc-java","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LamGC%2Fjsonrpc-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LamGC%2Fjsonrpc-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LamGC%2Fjsonrpc-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LamGC%2Fjsonrpc-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LamGC","download_url":"https://codeload.github.com/LamGC/jsonrpc-java/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246100580,"owners_count":20723479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-rpc","jsonrpc","jsonrpc-lib","rpc-library"],"created_at":"2025-02-03T07:47:07.184Z","updated_at":"2025-03-28T21:15:37.931Z","avatar_url":"https://github.com/LamGC.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonrpc-java\n\n## 简介\n\n本库仅用于实现 JsonRpc 的基本对象组成、序列化和便于使用所需的少量工具类，并不实现任何传输细节。\n\n简单来讲，这个库只负责帮你序列化和反序列化 RPC 报文，至于怎么传输，就看你的喜好了。\n\n\u003e JSON-RPC 2.0 没有定义任何特定于传输的问题，因为传输和 RPC 是独立的。 ——Roland Koebler\n\n你可以通过这个库将某个 JsonRpc请求序列化成符合规范的 JsonRpc 请求报文，\n然后通过你实现的数据传输链路将报文传递给 RPC 服务端，然后通过本库将 JsonRpc 请求报文反序列化成一样的 JsonRpc 请求对象。  \n反过来，你也可以通过本库将 JsonRpc 响应序列化，然后通过你实现的数据传输链路将报文传递给 RPC 客户端。\n\n这么做可以确保 RPC 跟传输的独立性，让你无需担心规范问题的同时，可以自由选择使用何种传输链路传输 RPC 报文，\n比如网络、蓝牙、二维码甚至是让人帮你传话！\n\n本依赖库以确保按 [JsonRpc 2.0 规范](https://www.jsonrpc.org/specification) 进行开发，并使用足够的测试项目以确保符合规范。\n\n## 安装\n\n依赖库中有关序列化的操作基本使用了 Gson，这么做主要是作者的习惯（我是 Gson 爱好者 :P）。  \n首先，引入依赖库作为你项目的依赖项：\n\n```kotlin\ndependencies {\n    implementation(\"net.lamgc:jsonrpc-java:0.1.0-RC1\")\n}\n```\n\n或者用 Groovy 的方式导入：\n\n```groovy\ndependencies {\n    implementation 'net.lamgc:jsonrpc-java:0.1.0-RC1'\n}\n```\n\nMaven 的话可以这样：\n\n```xml\n\n\u003cdependency\u003e\n    \u003cgroupId\u003enet.lamgc\u003c/groupId\u003e\n    \u003cartifactId\u003ejsonrpc-java\u003c/artifactId\u003e\n    \u003cversion\u003e0.1.0-RC1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n由于还没正式发布 1.0.0 版本, 因此你需要将我的 Gitea\n服务器加入到你的项目仓库列表中，具体看这里：[net.lamgc:jsonrpc-java 软件包](https://git.lamgc.me/LamGC/-/packages/maven/net.lamgc-jsonrpc-java/0.1.0-rc1)\n\n## 使用\n\n创建 Gson，将 `JsonRpcRequestSerializer` 和 `JsonRpcResponseSerializer`\n注册为 `JsonRpcRequest` 和 `JsonRpcResponse` 的类型适配器，然后启用 Null 序列化：\n\n```java\nGson gson = new GsonBuilder()\n        .registerTypeAdapter(JsonRpcRequest.class, new JsonRpcRequestSerializer())\n        .registerTypeAdapter(JsonRpcResponse.class, new JsonRpcResponseSerializer())\n        .serializeNulls()\n        .create();\n\n// 或者，使用依赖库提供的工具类快速创建一个, \n// 如果你需要定制, 只需要调用 gson.newGsonBuilder() 即可.\nGson gson = JsonRpcUtils.createGsonForJsonRpc();\n```\n\n\u003e 注意：如果不启用 Null 序列化，那么 JsonRpcResponse 将无法对错误的 JsonRpcRequest\n\u003e 生成正确的响应。（[相关规范](https://www.jsonrpc.org/specification#:~:text=If%20there%20was%20an%20error%20in%20detecting%20the%20id%20in%20the%20Request%20object%20(e.g.%20Parse%20error/Invalid%20Request)%2C%20it%20MUST%20be%20Null.)）\n\n如果有需要，将你所需要的类型适配器也注册进去。\n\n然后将 RPC 接口的实现绑定到一个 `JsonRpcExecutor` 中：\n\n```java\nObject impl = new RemoteInterfaceImpl();\nJsonRpcExecutor rpcExecutor = new SimpleJsonRpcExecutor(impl, gson);\n```\n\n然后尝试接收一个请求，并传入 JsonRpcExecutor 进行处理，并返回给调用方：\n\n```java\nJsonRpcRequest request = gson.fromJson(json, JsonRpcRequest.class);\nJsonRpcResponse response = rpcExecutor.execute(request);\nString json = gson.toJson(response);\n// 然后传回给客户端.\nrequest.sendResponse(json);\n```\n\n## 许可证\n\n```text\nCopyright 2023 LamGC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamgc%2Fjsonrpc-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamgc%2Fjsonrpc-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamgc%2Fjsonrpc-java/lists"}