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: encoding/json: add *json.Decoder to return type of DisallowUnknownFields #40443

Closed
KaiserKarel opened this issue Jul 28, 2020 · 1 comment
Labels
Milestone

Comments

@KaiserKarel
Copy link

@KaiserKarel KaiserKarel commented Jul 28, 2020

For strict json parsing using encoding/json, one has to write the following quite often:

var mystruct Mystruct
decoder := json.NewDecoder(request.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&mystruct)

Instead, being able to chain these calls makes the API more pleasant to use:

var mystruct Mystruct
err := json.NewDecoder(request.Body)
    .DisallowUnknownFields()
    .Decode(&mystruct)

It's also less error prone, as the decoder cannot be reused.

Technically this is a breaking change, since the DisallowUnknownFields might by passed as a function pointer, although I cannot imagine any use for that. Alternatively, We could add a Strict method to the decoder, to avoid breaking existing code.

@gopherbot gopherbot added this to the Proposal milestone Jul 28, 2020
@gopherbot gopherbot added the Proposal label Jul 28, 2020
@ianlancetaylor ianlancetaylor changed the title proposal: add *json.Decoder to return type of DisallowUnknownFields proposal: encoding/json: add *json.Decoder to return type of DisallowUnknownFields Aug 7, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 7, 2020

We can agree or disagree as to whether this would be a good idea, but it doesn't matter because we can't make this change. It would break the Go 1 compatibility guarantee (https://golang.org/doc/go1compat). Sorry.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.