Serialize a Rust Struct into a JSON String with serde_json
Make It JSON
This is how I'm converting Rust structs into JSON strings:
---
serde =
serde_json = "1.0.140"
---
use Serialize;
Output:
{
"alfa": "the quick brown fox",
"bravo": [
"jumps over",
"the lazy dog"
]
}
Notes
-
I'm using
serde_json::to_string_pretty()
for the example to pretty print it. -
There is also
serde_json::to_string()
if you don't need the nice formatting (e.g. if you're using it for data instead of display)
Nice and straight forward. Gotta love it.
-a
-- end of line --