golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.13k stars 17.46k forks source link

encoding/json: document that Encoder.Encode produces JSON values as single text line #67943

Closed dolmen closed 1 month ago

dolmen commented 2 months ago

Proposal Details

The encoding/json Encoder outputs JSON values as single lines of text since its original version and this is a design choice (see @rsc claim). This makes it an early implementation of the JSON Lines standard.

However this feature is undocumented: nothing about the absence of \n in doc for Encoder, Encoder.Encode (or json.Marshal). As this is undocumented, users can't rely on this implementation detail (but I expect many rely on it anyway).

This proposal is about extending the Encoder.Encode documentation to guarantee that no raw \n or \r is produced inside a JSON value.

gabyhelp commented 2 months ago

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

mauri870 commented 2 months ago

I don't think this needs to be a proposal as it only affects documentation. Taking it out of the proposal process.

mauri870 commented 2 months ago

cc @dsnet @mvdan per https://dev.golang.org/owners

dolmen commented 2 months ago

I marked this as a proposal because it engages the Go project to preserve the behaviour in the future, so it might need discussing instead of just discussing the wording in a CL.

Also: I don't know if there are already regression tests that enforce this feature. So this might be a bit more than just documentation.

ianlancetaylor commented 2 months ago

We basically can't change this kind of thing at this point anyhow. We don't need a proposal to retain the current behavior.

dsnet commented 2 months ago

Agreed. Let's document this. I think the behavior is reasonable as a default, but there are some people that would like to avoid the newlines, but that can be a potential option in v2. See https://github.com/go-json-experiment/json/issues/40 for context.

gopherbot commented 1 month ago

Change https://go.dev/cl/597976 mentions this issue: encoding/json: document compact json output in Encoder.Encode