53 stable releases (18 major)

18.0.0 Jul 20, 2021
17.1.0 Jun 7, 2021
17.0.0 Jan 20, 2021
16.0.0 Dec 14, 2020
0.2.0 Jan 19, 2016

#34 in Web programming

Download history 38467/week @ 2023-11-21 46582/week @ 2023-11-28 46693/week @ 2023-12-05 52577/week @ 2023-12-12 46894/week @ 2023-12-19 24060/week @ 2023-12-26 48924/week @ 2024-01-02 50773/week @ 2024-01-09 59349/week @ 2024-01-16 52880/week @ 2024-01-23 56899/week @ 2024-01-30 59897/week @ 2024-02-06 48801/week @ 2024-02-13 46767/week @ 2024-02-20 46821/week @ 2024-02-27 41421/week @ 2024-03-05

195,436 downloads per month
Used in 720 crates (119 directly)

MIT license

62KB
1.5K SLoC

Parity JSON-RPC

NOTE: This crate is no longer actively developed; please have a look at our jsonrpsee crate if you're looking for an actively maintained JSON RPC implementation.

Rust implementation of JSON-RPC 2.0 Specification. Transport-agnostic core and transport servers for http, ipc, websockets and tcp.

New! Support for clients.

Documentation

Sub-projects

Examples

Basic Usage (with HTTP transport)

use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::ServerBuilder;

fn main() {
	let mut io = IoHandler::default();
	io.add_method("say_hello", |_params: Params| async {
		Ok(Value::String("hello".to_owned()))
	});

	let server = ServerBuilder::new(io)
		.threads(3)
		.start_http(&"127.0.0.1:3030".parse().unwrap())
		.unwrap();

	server.wait();
}

Basic usage with derive

use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

#[rpc]
pub trait Rpc {
	/// Adds two numbers and returns a result
	#[rpc(name = "add")]
	fn add(&self, a: u64, b: u64) -> Result<u64>;
}

pub struct RpcImpl;
impl Rpc for RpcImpl {
	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}
}

fn main() {
	let mut io = jsonrpc_core::IoHandler::new();
	io.extend_with(RpcImpl.to_delegate())
}

Client support

use jsonrpc_core_client::transports::local;
use jsonrpc_core::{BoxFuture, IoHandler, Result};
use jsonrpc_core::futures::{self, future, TryFutureExt};
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
	/// Returns a protocol version
	#[rpc(name = "protocolVersion")]
	fn protocol_version(&self) -> Result<String>;

	/// Adds two numbers and returns a result
	#[rpc(name = "add", alias("callAsyncMetaAlias"))]
	fn add(&self, a: u64, b: u64) -> Result<u64>;

	/// Performs asynchronous operation
	#[rpc(name = "callAsync")]
	fn call(&self, a: u64) -> BoxFuture<Result<String>>;
}

struct RpcImpl;

impl Rpc for RpcImpl {
	fn protocol_version(&self) -> Result<String> {
		Ok("version1".into())
	}

	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}

	fn call(&self, _: u64) -> BoxFuture<Result<String>> {
		Box::pin(future::ready(Ok("OK".to_owned())))
	}
}

fn main() {
	let mut io = IoHandler::new();
	io.extend_with(RpcImpl.to_delegate());

	let (client, server) = local::connect::<gen_client::Client, _, _>(io);
	let fut = client.add(5, 6).map_ok(|res| println!("5 + 6 = {}", res));
	futures::executor::block_on(async move { futures::join!(fut, server) })
		.0
		.unwrap();
}

lib.rs:

Transport agnostic jsonrpc library.

Right now it supports only server side handling requests.

use jsonrpc_core::IoHandler;
use jsonrpc_core::Value;
let mut io = IoHandler::new();
io.add_sync_method("say_hello", |_| {
    Ok(Value::String("Hello World!".into()))
});

let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"Hello World!","id":1}"#;

assert_eq!(io.handle_request_sync(request), Some(response.to_string()));

Dependencies

~1.5–2.5MB
~52K SLoC