Skip to content

encoding/json/v2: make DiscardUnknownMembers and RejectUnknownMembers affect both marshaling and unmarshaling #76444

@dsnet

Description

@dsnet

Proposal Details

DiscardUnknownMembers is documented as only "affecting marshaling and is ignored when unmarshaling". However, there is an entirely reasonable semantic for unmarshaling, which is to avoid storing (i.e., skipping) unknown members even if a field tagged with unknown exists. We should support both marshal and unmarshal for consistency sake.

RejectUnknownMembers is documented as only "affecting unmarshaling and is ignored when marshaling". However, there is an entirely reasonable semantic for marshaling, which is to report an error if a field tagged with unknown exists and contains members. We should support both marshal and unmarshal for consistency sake.

This does call into question what happens when both DiscardUnknownMembers and RejectUnknownMembers are specified. We will follow the precedence documented in Options where latter options override previously set options.

Alternatively, we can merge DiscardUnknownMembers and RejectUnknownMembers into a single option that takes in an enum constant, but this is more API surface.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Needs review

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions