【发布时间】:2017-07-21 18:35:05
【问题描述】:
我想将 3rd 方库枚举与 JSON 相互转换。因为我不想编辑第 3 方源代码,所以我不想使用派生宏。
我想手写 serde_json deserialize 方法。我认为模式匹配是要走的路,但我需要匹配的东西不是公开的:
extern crate serde_json;
#[test]
fn test_parse_from_json() {
let s = r#"{
"e": 0,
"c": 1,
"n": 2
}"#;
let v: Map<String, Value> = serde_json::from_str(&s).unwrap();
match (&v["e"], &v["c"], &v["n"]) {
(&Value::Number(ref e), &Value::Number(ref c), &Value::Number(ref n)) => {
// e is a PosInt(u64) but that isn't public to match one nor access its methods!
let value = e.n; // error[E0616]: field `n` of struct `serde_json::Number` is private
}
_ => (),
}
}
那不编译。如果我用可以设置断点的东西替换那个内部位,我可以在调试器中看到e 是一个包含PosInt(0) 的Number。
【问题讨论】: