我有以下结构
#[derive(Serialize)]
pub struct MyStruct {
pub id: String,
pub score: f32,
pub json: String,
}
Run Code Online (Sandbox Code Playgroud)
该json字段始终包含已字符串化的有效 JSON 对象。
给定一个实例,我想使用 JSON 内容对其进行序列化。就像是:
let a = MyStruct {
id: "my-id".to_owned(),
score: 20.3,
json: r#"{
"ffo": 4
}"#,
};
let r = to_string(&a).unwrap();
assert_eq!(r, r#"{
"id": "my-id",
"score": 20.3,
"json": {
"ffo": 4
}
}"#);
Run Code Online (Sandbox Code Playgroud)
注意:我不需要支持不同的序列化格式,只需要支持 JSON。NB2:我确信该json字段始终包含有效的 JSON 对象。NB3:我通常使用 serde,但我愿意使用不同的库。
我怎样才能做到这一点?
编辑:如果可能的话,我想避免在序列化过程中反序列化字符串。