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

Comments

@KaiserKarel
Copy link

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
@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

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.

@golang golang locked and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants