Skip to content

Commit 64c0ed0

Browse files
committed
add support for u64 as string in the jsonrpc inventory
1 parent e6a5a9b commit 64c0ed0

File tree

1 file changed

+39
-1
lines changed
  • data_structures/src/chain

1 file changed

+39
-1
lines changed

data_structures/src/chain/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use ethereum_types::U256;
1515
use failure::Fail;
1616
use futures::future::BoxFuture;
1717
use ordered_float::OrderedFloat;
18-
use serde::{Deserialize, Serialize};
18+
use serde::{Deserialize, Deserializer, Serialize};
19+
use core::fmt::Display;
1920

2021
use partial_struct::PartialStruct;
2122
use witnet_crypto::{
@@ -1604,13 +1605,50 @@ pub struct ValueTransferOutput {
16041605
/// Address that will receive the value
16051606
pub pkh: PublicKeyHash,
16061607
/// Transferred value in nanowits
1608+
#[serde(
1609+
serialize_with = "u64_to_string",
1610+
deserialize_with = "number_from_string"
1611+
)]
16071612
pub value: u64,
16081613
/// The value attached to a time-locked output cannot be spent before the specified
16091614
/// timestamp. That is, they cannot be used as an input in any transaction of a
16101615
/// subsequent block proposed for an epoch whose opening timestamp predates the time lock.
16111616
pub time_lock: u64,
16121617
}
16131618

1619+
pub fn u64_to_string<S>(val: &u64, serializer: S) -> Result<S::Ok, S::Error>
1620+
where
1621+
S: serde::Serializer,
1622+
{
1623+
if serializer.is_human_readable() {
1624+
serializer.serialize_str(&val.to_string())
1625+
} else {
1626+
serializer.serialize_u64(*val)
1627+
}
1628+
}
1629+
1630+
pub fn number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error>
1631+
where
1632+
D: Deserializer<'de>,
1633+
T: FromStr + serde::Deserialize<'de>,
1634+
<T as FromStr>::Err: Display,
1635+
{
1636+
#[derive(Deserialize)]
1637+
#[serde(untagged)]
1638+
enum StringOrInt<T> {
1639+
String(String),
1640+
Number(T),
1641+
}
1642+
if deserializer.is_human_readable() {
1643+
match StringOrInt::<T>::deserialize(deserializer)? {
1644+
StringOrInt::String(s) => s.parse::<T>().map_err(serde::de::Error::custom),
1645+
StringOrInt::Number(i) => Ok(i),
1646+
}
1647+
} else {
1648+
T::deserialize(deserializer)
1649+
}
1650+
}
1651+
16141652
impl ValueTransferOutput {
16151653
#[inline]
16161654
pub fn value(&self) -> u64 {

0 commit comments

Comments
 (0)