Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround serde deserializing incorrectly when arbitrary_precision enabled #521

Merged
merged 6 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tls = ["jsonrpc-client-transports/tls"]
http = ["jsonrpc-client-transports/http"]
ws = ["jsonrpc-client-transports/ws"]
ipc = ["jsonrpc-client-transports/ipc"]
arbitrary_precision = ["jsonrpc-client-transports/arbitrary_precision"]

[dependencies]
jsonrpc-client-transports = { version = "14.0", path = "./transports", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions core-client/transports/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ipc = [
"jsonrpc-server-utils",
"tokio",
]
arbitrary_precision = []

[dependencies]
failure = "0.1"
Expand Down
29 changes: 19 additions & 10 deletions core-client/transports/src/transports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,25 @@ impl RequestBuilder {
pub fn parse_response(
response: &str,
) -> Result<(Id, Result<Value, RpcError>, Option<String>, Option<SubscriptionId>), RpcError> {
serde_json::from_str::<ClientResponse>(&response)
.map_err(|e| RpcError::ParseError(e.to_string(), e.into()))
.map(|response| {
let id = response.id().unwrap_or(Id::Null);
let sid = response.subscription_id();
let method = response.method();
let value: Result<Value, Error> = response.into();
let result = value.map_err(RpcError::JsonRpcError);
(id, result, method, sid)
})
// https://github.com/serde-rs/json/issues/505
// Arbitrary precision confuses serde when deserializing into untagged enums,
// this is a workaround
let response = if cfg!(feature = "arbitrary_precision") {
let value =
serde_json::from_str::<Value>(&response).map_err(|e| RpcError::ParseError(e.to_string(), e.into()))?;
serde_json::from_value::<ClientResponse>(value).map_err(|e| RpcError::ParseError(e.to_string(), e.into()))
} else {
serde_json::from_str::<ClientResponse>(&response).map_err(|e| RpcError::ParseError(e.to_string(), e.into()))
};

response.map(|response| {
let id = response.id().unwrap_or(Id::Null);
let sid = response.subscription_id();
let method = response.method();
let value: Result<Value, Error> = response.into();
let result = value.map_err(RpcError::JsonRpcError);
(id, result, method, sid)
})
}

/// A type representing all possible values sent from the server to the client.
Expand Down