jsonlrpc

Crates.iojsonlrpc
lib.rsjsonlrpc
version
sourcesrc
created_at2024-09-05 14:08:35.459397
updated_at2024-12-02 12:49:10.456785
descriptionA JSON-RPC 2.0 library that streams JSON objects in JSON Lines format
homepagehttps://github.com/sile/jsonlrpc
repositoryhttps://github.com/sile/jsonlrpc
max_upload_size
id1364686
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Takeru Ohta (sile)

documentation

README

jsonlrpc

jsonlrpc Documentation Actions Status License

A JSON-RPC 2.0 library that streams JSON objects in JSON Lines format.

Examples

RPC client:

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:

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
}
Commit count: 45

cargo fmt