Double quotes around JSON keys in Serde


#1

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: []


#2

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


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

#4

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


#5

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


#6

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.


#7

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.


#8

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\":

#9

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


#10

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.


#11

I all is well now. Thanks for the input.