Skip to content

Commit

Permalink
upgrade to serde_with 2.0 (#2735)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranrund authored Oct 20, 2022
1 parent 55e53f5 commit 6e5ef62
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
59 changes: 57 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion util/serial/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ prost = { version = "0.11", default-features = false, features = ["prost-derive"
protobuf = { version = "2.27", optional = true }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
serde_cbor = { version = "0.11.1", default-features = false, features = ["alloc"] }
serde_with = { version = "1.14", default-features = false, optional = true }
serde_with = { version = "2.0", default-features = false, features = ["macros"], optional = true }

[dev-dependencies]
serde_json = "1.0"
12 changes: 10 additions & 2 deletions util/serial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub fn decode<T: Message + Default>(buf: &[u8]) -> Result<T, DecodeError> {

#[cfg(feature = "serde_with")]
mod json_u64 {

use super::*;
use serde_with::{serde_as, DisplayFromStr};

/// Represents u64 using string, when serializing to Json
/// Javascript integers are not 64 bit, and so it is not really proper json.
Expand All @@ -89,9 +89,10 @@ mod json_u64 {
///
/// This does not rely on the serde-json arbitrary precision feature, which
/// (we fear) might break other things (e.g. https://github.com/serde-rs/json/issues/505)
#[serde_as]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Hash, Serialize)]
#[serde(transparent)]
pub struct JsonU64(#[serde(with = "serde_with::rust::display_fromstr")] pub u64);
pub struct JsonU64(#[serde_as(as = "DisplayFromStr")] pub u64);

impl From<&u64> for JsonU64 {
fn from(src: &u64) -> Self {
Expand Down Expand Up @@ -198,5 +199,12 @@ mod json_u64_tests {
let serialized = serialize(&the_struct).unwrap();
let deserialized: TestStruct = deserialize(&serialized).unwrap();
assert_eq!(deserialized, the_struct);

// Sanity that serde_as works as expected: it should accept and hand us back
// strings.
let expected_json =
r#"{"nums":["0","1","2","18446744073709551615"],"block":"18446744073709551614"}"#;
assert_eq!(expected_json, serde_json::to_string(&the_struct).unwrap());
assert_eq!(the_struct, serde_json::from_str(expected_json).unwrap());
}
}

0 comments on commit 6e5ef62

Please sign in to comment.