Skip to content

add support for u64 as string in the jsonrpc inventory #2620

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all 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
40 changes: 39 additions & 1 deletion data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use ethereum_types::U256;
use failure::Fail;
use futures::future::BoxFuture;
use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use core::fmt::Display;

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

pub fn u64_to_string<S>(val: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_str(&val.to_string())
} else {
serializer.serialize_u64(*val)
}
}

pub fn number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: FromStr + serde::Deserialize<'de>,
<T as FromStr>::Err: Display,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrInt<T> {
String(String),
Number(T),
}
if deserializer.is_human_readable() {
match StringOrInt::<T>::deserialize(deserializer)? {
StringOrInt::String(s) => s.parse::<T>().map_err(serde::de::Error::custom),
StringOrInt::Number(i) => Ok(i),
}
} else {
T::deserialize(deserializer)
}
}

impl ValueTransferOutput {
#[inline]
pub fn value(&self) -> u64 {
Expand Down
Loading