我有以下宏。请注意,StringContent
是枚举项。
#[macro_export]
macro_rules! from_str {
($json:expr) => {
StringContent(String::from($json))
}
}
允许我编写像
这样的代码from_str!(r#"{
"appName": "Demo App",
"appVersion": "1.0",
"database": {
"host": "dev.database.com",
"port": 3000
}
}"#)
现在我想要另一个宏from_json!
,它可以让我摆脱r#""#
这样的
from_json!({
"appName": "Demo App",
"appVersion": "1.0",
"database": {
"host": "dev.database.com",
"port": 3000
}
})
我已尝试过以下似乎不起作用的
#[macro_export]
macro_rules! from_json {
($t:tt) => {
StringContent(String::from(concat!(r#"r#""#, stringify!($t), r#"""# , r#"#"#)))
}
}
如何让from_json
工作?
答案 0 :(得分:5)
您的宏不起作用,因为concat!
不能用于以语法合理的方式将标识符附加到彼此。它改为将标识符连接成一个字符串。您现在看起来像"r#\" ~your JSON~ \"#"
,其中r#
和#
是文字字符。
在a stabilized, extended concat_idents!
is implemented之前,您的方法无效。
您必须在宏中手动解析JSON语法。如需灵感,请查看how Serde does it。
总的来说,{p> serde_json似乎很适合您的用例。如果可能的话,我建议删除任何JSON解析的自定义实现,而不是使用serde_json,因为它是Rust中所有JSON事实的标准选择。这是如何使用serde_json将JSON转换为原始字符串的最小示例:
#[macro_use]
extern crate serde_json;
fn main() {
let as_json_value = json!({
"appName": "Demo App",
"appVersion": "1.0",
"database": {
"host": "dev.database.com",
"port": 3000
}
});
let as_string = format!("{}", as_json_value);
println!("{}", as_string);
}
虽然您可能希望重写从StringContent
构建的serde_json::Value
枚举,因为它已经为您整理好了。
答案 1 :(得分:3)
我只使用json
macro provided by serde_json,它使用您的确切语法:
#[macro_use]
extern crate serde_json;
extern crate serde;
fn main() {
let x = json!({
"appName": "Demo App",
"appVersion": "1.0",
"database": {
"host": "dev.database.com",
"port": 3000
}
});
}
这会创建一个Value
结构。如果由于某种原因你真的需要它作为字符串,你需要使用它Display
实现重新序列化它:
extern crate serde;
#[macro_use]
extern crate serde_json;
struct StringContent(String);
macro_rules! from_json {
($x:tt) => {{
StringContent(json!($x).to_string())
}}
}
fn main() {
let x = from_json!({
"appName": "Demo App",
"appVersion": "1.0",
"database": {
"host": "dev.database.com",
"port": 3000
}
});
println!("{}", x.0)
}