samscott89 / serde_qs

Serde support for querystring-style strings
Apache License 2.0
193 stars 68 forks source link

Serializing unit type fails #72

Closed valkum closed 1 year ago

valkum commented 1 year ago

When serializing a unit or struct unit "serde primitive", serde_qs fails with the following error: tried to serialize a value before serializing key

When looking at serde_json I think the right thing to do would be to return an empty string in serialize_unit but I am not sure if that breaks the whole serialization implementation.

https://play.integer32.com/?version=stable&mode=debug&edition=2021&gist=5e91db9aa9b04935c6e3a2f2c41a3ce2 (play.rust-lang.org seems to be down)

Compare with

use serde::Serialize;

fn main() {
  println!("{}", serde_qs::to_string(&()).unwrap()); // I guess should yield ""
  println!("{}", serde_qs::to_string(&A).unwrap()); // I guess should yield ""
  println!("{}", serde_qs::to_string(&B{t: ()}).unwrap()); // I guess should yield "t="
}

#[derive(Serialize)]
struct A;
#[derive(Serialize)]
struct B{t: ()}

I probably can provide a PR but would need information about the desired behavior.

samscott89 commented 1 year ago

🙏 thank you @valkum for the contribution and @djc for the poke :)

Shipped in 0.12