Option types in JSON, more tests.

This commit is contained in:
Gav Wood 2016-01-14 23:39:59 +01:00
parent 3006b8ac66
commit 6a7c823862
2 changed files with 37 additions and 2 deletions

View File

@ -195,6 +195,7 @@ macro_rules! impl_hash {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
match json { match json {
&Json::String(ref s) => { &Json::String(ref s) => {
println!("s: {}", s);
match s.len() % 2 { match s.len() % 2 {
0 => FromStr::from_str(clean_0x(s)).unwrap(), 0 => FromStr::from_str(clean_0x(s)).unwrap(),
_ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap() _ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap()

View File

@ -46,6 +46,16 @@ impl<T> FromJson for Vec<T> where T: FromJson {
} }
} }
impl<T> FromJson for Option<T> where T: FromJson {
fn from_json(json: &Json) -> Self {
match json {
&Json::String(ref o) if o.is_empty() => None,
&Json::Null => None,
_ => Some(FromJson::from_json(json)),
}
}
}
impl FromJson for u64 { impl FromJson for u64 {
fn from_json(json: &Json) -> Self { fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64() U256::from_json(json).low_u64()
@ -77,7 +87,7 @@ fn u256_from_json() {
} }
#[test] #[test]
fn h256_from_json_() { fn h256_from_json() {
let j = Json::from_str("{ \"with\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"without\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\" }").unwrap(); let j = Json::from_str("{ \"with\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"without\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\" }").unwrap();
let v: H256 = xjson!(&j["with"]); let v: H256 = xjson!(&j["with"]);
@ -95,9 +105,33 @@ fn vec_u256_from_json() {
} }
#[test] #[test]
fn vec_h256_from_json_() { fn vec_h256_from_json() {
let j = Json::from_str("{ \"array\": [ \"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"] }").unwrap(); let j = Json::from_str("{ \"array\": [ \"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"] }").unwrap();
let v: Vec<H256> = xjson!(&j["array"]); let v: Vec<H256> = xjson!(&j["array"]);
assert_eq!(vec![H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(); 2], v); assert_eq!(vec![H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(); 2], v);
} }
#[test]
fn simple_types() {
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
let v: u16 = xjson!(&j["int"]);
assert_eq!(42u16, v);
let v: u32 = xjson!(&j["dec"]);
assert_eq!(42u32, v);
let v: u64 = xjson!(&j["hex"]);
assert_eq!(42u64, v);
}
#[test]
fn option_types() {
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
let v: Option<u16> = xjson!(&j["int"]);
assert_eq!(Some(42u16), v);
let v: Option<u16> = xjson!(&j["dec"]);
assert_eq!(Some(42u16), v);
let v: Option<u16> = xjson!(&j["null"]);
assert_eq!(None, v);
let v: Option<u16> = xjson!(&j["empty"]);
assert_eq!(None, v);
}