Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitting newline at end when writing JSON to file with to_writer_pretty #716

Closed
linclelinkpart5 opened this issue Oct 4, 2020 · 2 comments

Comments

@linclelinkpart5
Copy link

Hello, I'm having trouble figuring out how to emit a newline at the end of a file write using serde_json. These JSON files I'm serializing get added to a git repo, and diff complains about the lack of newlines at the end. Not to mention, cat-ing these files out messes with the display.

I'd love for there to be a flag on the *Formatter traits to allow this, but there doesn't seem to be one. Is there an easy fix that I'm overlooking? Thanks!

@dtolnay
Copy link
Member

dtolnay commented Oct 15, 2020

I would prefer not to introduce a formatter flag for this. You could write the newline like this:

use std::io::Write;

serde_json::to_writer_pretty(&mut writer, value)?;
writer.write_all(b"\n")?;

@piegamesde
Copy link

I think that ending the output with a newline should be the default behavior for pretty formatting. I don't think there are many situations where one would want pretty formatting but without a trailing newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants