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

Json files are not newline terminated when pretty printing #147

Closed
u3shit opened this issue Dec 6, 2023 · 3 comments
Closed

Json files are not newline terminated when pretty printing #147

u3shit opened this issue Dec 6, 2023 · 3 comments
Labels
discussion enhancement New feature or request

Comments

@u3shit
Copy link

u3shit commented Dec 6, 2023

Describe the bug
When writing a json file using yyjson_mut_write_fp (and probably others but I haven't tested them), even when using YYJSON_WRITE_PRETTY or YYJSON_WRITE_PRETTY_TWO_SPACES, the resulting file will have no newline at the end. While it might be justifiable with minimized output, according to POSIX, these are not text files (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 and https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206) which produce various problems when trying to use these files. E.g. if you cat one of these files, the closing } will end up before your shell prompt but the shell will think it's not there and mess up cursor movement commands, diff files will have an ugly \ No newline at end of file at the end, busybox based utilities sometimes ignore the last line in these files (which is technically correct, since you're feeding binary garbage into an utility that expects a text file), etc.

Your environment

  • OS: Gentoo Linux amd64
  • Compiler: gcc 13.2.1
@ibireme
Copy link
Owner

ibireme commented Dec 7, 2023

Most JSON libraries don't add a newline at the end of JSON files. It's a topic that's been talked about, like here: Why is json.dump not ending the line with '\n'?.

If you want, we can add a new option to control it, like YYJSON_WRITE_NEWLINE_AT_END .

@ibireme ibireme added enhancement New feature or request discussion labels Dec 7, 2023
@u3shit
Copy link
Author

u3shit commented Dec 8, 2023

I see. jq on the other hand adds the trailing \n. I'm also fine with an option.
(Right now I'm only using the fp version because I'm writing to stdout in my toy example now, but later I want to move to files where I can't just putchar('\n') after yyjson_mut_write_file.)

@ibireme
Copy link
Owner

ibireme commented Dec 11, 2023

Added.

@ibireme ibireme closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants