The use case I would use this for is nicely formatted API request bodies with syntax error checking, but just returning the raw string inside instead of incurring a runtime serialization cost.
Instead of:
client
.post("https://httpbin.org/post")
.header("Content-Type", "application/json")
.body(serde_json::to_string(&json!({
"test": "value"
}))?)
.send()
.await?;
We could do
let body: &'static str = serde_json::json_str!({
"test": "value"
});
client
.post("https://httpbin.org/post")
.header("Content-Type", "application/json")
.body(body.to_owned())
.send()
.await?;
It would be nice if there was a form that accepted a Struct that implements Serialize and verifies that the string matches it at compile time, though that might be beyond the scope of what serde can do at compile time.
Would be happy to implement myself but would love some direction from someone more familiar with the codebase