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

Inconsistent error on similar ways to achieve json encode #19

Closed
dimasdanz opened this issue Aug 1, 2022 · 2 comments
Closed

Inconsistent error on similar ways to achieve json encode #19

dimasdanz opened this issue Aug 1, 2022 · 2 comments

Comments

@dimasdanz
Copy link

Code sample

type request struct {
  ID string `json:"id"`
}

r := request{ID: "ID"}

body := bytes.NewBuffer(nil)
_ = json.NewEncoder(body).Encode(r) // linter reports error on this line

marshalled, _ := json.Marshal(r) // linter does not report error here
body := bytes.NewBuffer(marshalled)

json.NewEncoder(body).Encode(r) should not report error. because last 2 lines essentially does the same thing.

@breml
Copy link
Owner

breml commented Aug 2, 2022

Hi @dimasdanz

Thanks for your report. Originally, errchkjson worked the way you are describing. But in #14 we discussed this issue and we found, that json.NewEncoder(...).Encode() has some additional error paths, not related to the json encoding it self, but to the operations on the io.Writer. Therefore it is not save to omit the errors in this case.

@dimasdanz
Copy link
Author

Closing this, as I decided to just use json.Marshal whenever possible.

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

No branches or pull requests

2 participants