Skip to content

[Feature request] Variant of json! named like json_string! that returns raw string instead of serde_json::Value #1297

@KennanHunter

Description

@KennanHunter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions