Double quotes around JSON keys in Serde

Why doesn't Serde JSON output keys surrounded by double quotes by default? And how do I get this to work? Plus I'm also getting Type information, which I don't want, eg Head, Body, Outline.

(If I could pretty print this below somehow, I would)

{ version: "1.0", head: Head { title: "Fearless Rust Bloggers", owner_name: "Ray Scott", owner_email: "ray@gmail.com"
}, body: Body { outline: Outline { outlines: [Outline { outlines:

This isn't JSON, it looks like #[derive(Debug)] output. How are you generating it?

let serialized = serde_json::to_string(&opml).unwrap();
if let Ok(mut outfile) = File::create(name) {
    outfile.write_all(serialized.as_bytes()); 
}

That doesn't look right; to_string() returns a Result which doesn't have as_bytes().

My text marking failed me. There is actually a call to unwrap().

I guarantee the output you showed is not being written by the lines of code you gave. As mentioned, somewhere you are writing out #[derive(Debug)] output and bypassing the part that writes JSON output. It is not going to be possible for anyone to troubleshoot without more of the code available.

1 Like

I’ll have a look at the code before that then. Does the order of #[derive()] entries make a difference? One of them is Debug on the structs I’m working with.

The next statement after the ones I've already shown is:

println!("{:?}", serialized);

and the first bit of output from that is:

"{\"version\":\"1.0\",\"head\":{\"title\":\"Fearless Rust Bloggers\",\"ownerName\":\"Ray Scott\",\"ownerEmail\":\"ray@gmail.com\"},\"body\":

Is name what you think it is? Perhaps the File::open is failing, or writing to a place you're not expecting?

I think this is not so clever, even just hacking around...

I think the error must be there, because the file isn't be overwritten.

I all is well now. Thanks for the input.