Is encoding/json POSIX compatible?

130 views
Skip to first unread message

Xabi Martí

unread,
Jan 30, 2024, 9:16:35 AMJan 30
to golang-nuts
I'm writing a program that uses enconding/json and when writing the files I see that it doesn't add a newline character at the end of the file, according to https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206 it is supposed that a line is:

3.206 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

so, I've to add it manually? why is not POSIX compatible? 

example:

cat -e file.json | tail -n 2
  }$
}%

any help? thanks!

Brian Candler

unread,
Jan 30, 2024, 9:50:19 AMJan 30
to golang-nuts
The JSON spec does not require any whitespace after the object, newline or otherwise. And POSIX does not require that files end with a newline.

Maybe you are thinking of another spec, like https://jsonlines.org/ - but that's not part of JSON.

Javier Marti

unread,
Jan 30, 2024, 10:20:00 AMJan 30
to Brian Candler, golang-nuts
So, as per the link that I sent, a json file is not a text file, right? is just a file, I want to have this clear


thanks

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/qjxJ3Zunzfc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/692bd65e-74f4-448a-b399-de8c38785aefn%40googlegroups.com.

Brian Candler

unread,
Jan 30, 2024, 10:54:57 AMJan 30
to golang-nuts
By the definition of "3.403 Text File" then no, because a JSON file can have arbitrarily long lines.

Note also: "Although POSIX.1-2017 does not distinguish between text files and binary files (see the ISO C standard), many utilities only produce predictable or meaningful output when operating on text files"

Hence there's no guarantee that a line-based text processing utility like grep or sed will behave meaningfully with arbitrary JSON data. Typically they do work with "incomplete" lines (3.195), but not arbitrarily long ones.

Jason Phillips

unread,
Jan 30, 2024, 6:29:23 PMJan 30
to golang-nuts
More generally, the input and output of encoding/json need not be a file at all.
Reply all
Reply to author
Forward
0 new messages