jsonrpc_core/
lib.rs

1//! ### Transport agnostic jsonrpc library.
2//!
3//! Right now it supports only server side handling requests.
4//!
5//! ```rust
6//! use jsonrpc_core::IoHandler;
7//! use jsonrpc_core::Value;
8//! let mut io = IoHandler::new();
9//! io.add_sync_method("say_hello", |_| {
10//!     Ok(Value::String("Hello World!".into()))
11//! });
12//!
13//! let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
14//! let response = r#"{"jsonrpc":"2.0","result":"Hello World!","id":1}"#;
15//!
16//! assert_eq!(io.handle_request_sync(request), Some(response.to_string()));
17//! ```
18
19#![deny(missing_docs)]
20
21use std::pin::Pin;
22
23#[macro_use]
24extern crate log;
25#[macro_use]
26extern crate serde_derive;
27
28#[cfg(feature = "futures")]
29pub use futures;
30#[cfg(feature = "futures-executor")]
31pub use futures_executor;
32pub use futures_util;
33
34#[doc(hidden)]
35pub extern crate serde;
36#[doc(hidden)]
37pub extern crate serde_json;
38
39mod calls;
40mod io;
41
42pub mod delegates;
43pub mod middleware;
44pub mod types;
45
46/// A Result type.
47pub type Result<T> = std::result::Result<T, Error>;
48
49/// A `Future` trait object.
50pub type BoxFuture<T> = Pin<Box<dyn std::future::Future<Output = T> + Send>>;
51
52pub use crate::calls::{
53	Metadata, RemoteProcedure, RpcMethod, RpcMethodSimple, RpcMethodSync, RpcNotification, RpcNotificationSimple,
54	WrapFuture,
55};
56pub use crate::delegates::IoDelegate;
57pub use crate::io::{
58	Compatibility, FutureOutput, FutureResponse, FutureResult, FutureRpcResult, IoHandler, IoHandlerExtension,
59	MetaIoHandler,
60};
61pub use crate::middleware::{Middleware, Noop as NoopMiddleware};
62pub use crate::types::*;
63
64use serde_json::Error as SerdeError;
65
66/// workaround for https://github.com/serde-rs/json/issues/505
67/// Arbitrary precision confuses serde when deserializing into untagged enums,
68/// this is a workaround
69pub fn serde_from_str<'a, T>(input: &'a str) -> std::result::Result<T, SerdeError>
70where
71	T: serde::de::Deserialize<'a>,
72{
73	if cfg!(feature = "arbitrary_precision") {
74		let val = serde_json::from_str::<Value>(input)?;
75		T::deserialize(val)
76	} else {
77		serde_json::from_str::<T>(input)
78	}
79}