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

proposal: json: add Encoder.EncodeIndent #23508

Closed
blockloop opened this issue Jan 22, 2018 · 5 comments
Closed

proposal: json: add Encoder.EncodeIndent #23508

blockloop opened this issue Jan 22, 2018 · 5 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Proposal Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@blockloop
Copy link

What version of Go are you using (go version)?

1.9.2

What operating system and processor architecture are you using (go env)?

macOS/amd64

The encoding/json package has a very useful method MarshalIndent which allows users to create indented (pretty-printed) JSON. However when creating a json.Encoder there is no one-shot method for EncodeIndent. We have the ability to

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
enc.Encode(&data)

However, a one-shot method like EncodeIndent(&data, "", " ") would make this code much more concise and follow the same format as MarshalIndent. The above code would change to

json.NewEncoder(os.Stdout).EncodeIndent(&data, "", "  ")
@blockloop
Copy link
Author

Perhaps this was premature and I should have created this issue first, but given the simplicity of the change I already submitted a change. If so, that's okay. No hard feelings.

@mvdan mvdan changed the title Add (*json.Encoder).EncodeIndent json: add Encoder.EncodeIndent Jan 22, 2018
@mvdan mvdan added Suggested Issues that may be good for new contributors looking for work to do. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jan 22, 2018
@mvdan
Copy link
Member

mvdan commented Jan 22, 2018

The purpose of MarshalIndent is that one can go from the data structure to the JSON bytes in a single call. You go from 3 calls (NewEncoder, SetIndent, Encode) to just one.

In that sense, EncodeIndent makes less sense. You go from three to two.

In my opinion, if you need a custom encoder, an extra line for the indentation setting doesn't really add complexity. However, adding an extra method to Encoder does add complexity to the package's API.

@blockloop
Copy link
Author

blockloop commented Jan 22, 2018

I always approach both of these outcomes with the same request: "I have this data structure that needs to be marshaled with indention." The only distinguishing factor is "do I already have a Writer?" It is something that I believe is common enough for everyone hence the MarshalIndent function which provides something often used across projects.

However, I find myself needing a direct Encode to an existing io.Writer often enough that warrants a similar function to MarshalIndent that writes to an io.Writer. Perhaps this isn't the correct implementation. Would it be unreasonable for another package-level func that provided an indented marshaling to an io.Writer?

@dsnet dsnet changed the title json: add Encoder.EncodeIndent proposal: json: add Encoder.EncodeIndent Jan 22, 2018
@gopherbot gopherbot added this to the Proposal milestone Jan 22, 2018
@bradfitz
Copy link
Contributor

Go generally doesn't optimize for writing one-liners.

@rsc
Copy link
Contributor

rsc commented Jan 22, 2018

As Brad said, it's just one line. It's not worth the complexity of the new API here. If you find yourself writing

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
enc.Encode(&data)

all the time, then instead of writing

json.NewEncoder(os.Stdout).EncodeIndent(&data, "", "  ")

use a custom wrapper that makes that even shorter.

@rsc rsc closed this as completed Jan 22, 2018
@golang golang locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Proposal Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

5 participants