https://github.com/sile/jsonlrpc
A JSON-RPC 2.0 library that streams JSON objects in JSON Lines format.
https://github.com/sile/jsonlrpc
jsonl jsonrpc rust
Last synced: 16 days ago
JSON representation
A JSON-RPC 2.0 library that streams JSON objects in JSON Lines format.
- Host: GitHub
- URL: https://github.com/sile/jsonlrpc
- Owner: sile
- License: mit
- Created: 2024-09-05T05:13:09.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-12-02T12:49:14.000Z (5 months ago)
- Last Synced: 2025-03-25T07:12:35.214Z (about 1 month ago)
- Topics: jsonl, jsonrpc, rust
- Language: Rust
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
jsonlrpc
========[](https://crates.io/crates/jsonlrpc)
[](https://docs.rs/jsonlrpc)
[](https://github.com/sile/jsonlrpc/actions)
A [JSON-RPC 2.0] library that streams JSON objects in [JSON Lines] format.
[JSON-RPC 2.0]: https://www.jsonrpc.org/specification
[JSON Lines]: https://jsonlines.org/Examples
--------RPC client:
```rust
use std::net::TcpStream;
use jsonlrpc::{RpcClient, RequestId, RequestObject, ResponseObject, JsonRpcVersion};// Connect to a JSON-RPC server.
let server_addr = spawn_rpc_server_thread(); // See below
let socket = TcpStream::connect(server_addr).expect("failed to connect to server");
let mut client = RpcClient::new(socket);// Send a request to the server.
let request = RequestObject {
jsonrpc: JsonRpcVersion::V2,
id: Some(RequestId::Number(1)),
method: "foo".to_string(),
params: None,
};
let response = client.call(&request).expect("failed to RPC call");// Check the response.
let Some(ResponseObject::Ok { result, id, .. }) = response else {
panic!("expected ok response, got notification or err response")
};
assert_eq!(id, RequestId::Number(1));
```RPC server:
```rust
use std::net::{SocketAddr, TcpListener};
use jsonlrpc::{JsonlStream, JsonRpcVersion, RequestObject, ResponseObject};fn spawn_server_thread() -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind to address");
let addr = listener.local_addr().expect("failed to get local address");std::thread::spawn(move || {
for stream in listener.incoming() {
let stream = stream.expect("failed to accept incoming connection");
let mut stream = JsonlStream::new(stream);
std::thread::spawn(move || {
let request: RequestObject = stream.read_value().expect("failed to read request");
let response = ResponseObject::Ok {
jsonrpc: JsonRpcVersion::V2,
id: request.id.expect("expected request id"),
result: serde_json::Value::String(request.method),
};
stream.write_value(&response).expect("failed to write response");
});
}
});addr
}
```