Question:how to know if the struct I created Is correct for the json?and serialize it

I am new to rust so it would be great if there is some reference.

I have a json in my mind>
'''
{
"path":"pathtothatproj",
"defaultcmd":"cp",
"Cmdlist":[
"cp":{"windows":"someoscommand","linux":"someoscommand"},
.....

]
}
'''

I know that I need to Strongly typed json serialize,as mentioned in the readme,as the structure is kinda predictable

here is the struct rust code I have in my mind.
'''
//OSdependentcmdtorun
#[derive(Serialize, Deserialize, Debug)]
struct Onecmd {
windows: String,
linux: String
}

//
#[derive(Serialize, Deserialize, Debug)]
struct Config {
proj_path:String,
defaultcmd:String,
Cmdlist:Vec
}
'''

Is it correct?
I know how to convert the str to the json*struct.let varname: Config = serde_json::from_str(data)?;
but how to create this complex struct object and become the final json is kinda confusing.
Any reference would be helpful.thank you.

Please format your post and revise its language (eg., use punctuation, whitespace, and proper capitalization). It's very hard to read visually and it's not clear what you are asking either.

By default, derived impls of Serialize produce the exact same keys as the corresponding field name, so sou can just check if that is the case (in your example, it isn't at least for path and proj_path). Likewise, derived Deserialize impls expect field names to match map keys exactly by default. You can alter this behavior using various field- and type-level attributes (such as #[serde(rename = "...")]), which are clearly described in the documentation of Serde.

If you don't know how to instantiate structs, read the relevant part of the Book. Converting the resulting value to a JSON string is possible in a completely analogous manner, by calling serde_json::to_string().

noted will study and try your advice thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.