Skip to content

proposal: encoding/json: add *json.Decoder to return type of DisallowUnknownFields #40443

@KaiserKarel

Description

@KaiserKarel

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions