我刚开始使用 Rust,在反序列化方面遇到了一些麻烦。
我实际上是在尝试使用以下板条箱google_firestore1 中的ProjectDatabaseDocumentCreateDocumentCall函数。我想填充struct的字段。结构的文档很清楚,它期望 a作为值。fields
Document
HashMap<String, google_firestore1::Value>
问题是,如何将 JSON 字符串反序列化为HashMap<String, google_firestore1::Value>
?
这是我暂时写的代码:
extern crate google_firestore1 as firestore1;
use google_firestore1::Document;
use std::collections::HashMap;
use serde_json;
pub fn go() {
let _my_doc = Document::default();
let test = "{\"test\":\"test\", \"myarray\": [1]}";
// Working perfectly fine
let _working: HashMap<String, serde_json::Value> = serde_json::from_str(test).unwrap();
// Not working
let _not_working: HashMap<String, firestore1::Value> = serde_json::from_str(test).unwrap();
// Later I want to do the following
// _my_doc.fields = _not_working
}
显然这不起作用,它会因以下错误而崩溃。
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"test\", expected struct Value", line: 1, column: 14)', src/firestore.rs:17:85
stack backtrace:
当然,我注意到它serde_json::Value
和firestore1::Value
Struct 是不一样的。但是我查看了源代码,似乎firestore1::Value
正在实现 Deserialize 特性。
那么为什么它不起作用呢?在这种情况下,我需要遍历第一HashMap和反序列化serde_json::Value
到firestore1::Value
一遍吗?有没有更干净的方法来做我想做的事?
谢谢你的回答!
的定义firestore1::Value
是:
/// A message that can hold any of the supported value types.
///
/// This type is not used in any activity, and only used as *part* of another schema.
///
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct Value {
/// A bytes value.
///
/// Must not exceed 1 MiB - 89 bytes.
/// Only the first 1,500 bytes are considered by queries.
#[serde(rename="bytesValue")]
pub bytes_value: Option<String>,
/// A timestamp value.
///
/// Precise only to microseconds. When stored, any additional precision is
/// rounded down.
#[serde(rename="timestampValue")]
pub timestamp_value: Option<String>,
...
}
这意味着 a 的每个条目都firestore1::Value
必须是一个对象。我怀疑实际上只会设置一个字段,对应于值的实际类型(因为它们都是可选的)。
所以你的 json 需要是这样的:
let test = r#"{
"test":{"stringValue":"test"},
"myarray": {
"arrayValue":{"values":[{"integerValue":1}]}
}
}"#;
这是非常丑陋的,所以如果你做了很多自己的JSON到公司的FireStore交谈,我可能会从写一些助手转换serde_json::Value
到firestore1::Value
。
它可能看起来像这样:
fn my_firestore_from_json(v:serde_json::Value) -> firestore1::Value {
match v {
serde_json::Value::Null => firestore::Value {
// I don't know why this is a Option<String>
null_value: Some("".to_string),
..Default::default(),
},
serde_json::Value::Bool(b) => firestore::Value {
bool_value: Some(b),
..Default::default(),
},
// Implement this
serde_json::Value::Number(n) => my_firestore_number(n),
serde_json::Value::String(s) => firestore::Value {
string_value: Some(s),
..Default::default(),
},
serde_json::Value::Array(v) => firestore::Value {
array_value:
Some(firestore1::ArrayValue{
values:v.into_iter().map(my_firestore_from_json)
}),
..Default::default(),
},
// Implement this
serde_json::Value::Object(d) => my_firststore_object(/* something */)
}
}
如果From<T>
for有各种实现,这会更整洁一些firestore1::Value
,但是使用 的实现Default
使得这不会太难看。
还值得注意的是,并不是所有的 firebase 类型都在这里创建,因为 serde_json 中表达的类型与 firebase 支持的类型不同。
无论如何,这允许您通过执行以下操作来使用您的 JSON:
let test = "{\"test\":\"test\", \"myarray\": [1]}";
let working: HashMap<String, serde_json::Value> = serde_json::from_str(test).unwrap();
let value_map: HashMap<String, firestore1::Value> = working.iter().map(|(k,v)| (k, my_firestore_from_json(v)).collect();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句