Skip to content

feature: Collect unknown fields #707

Description

@makew0rld

Is your feature request related to a problem? Please describe.

Unmarshalling a document that supports arbitrary metadata alongside pre-defined keys.

Describe the solution you'd like

encoding/json/v2 supports a cool and useful feature that allows you to collect unknown object members (map key-value pairs) into a struct field.

type Color struct {
	Name  string
	Value string

	// Unknown is a Go struct field that holds unknown JSON object members.
	// It is marked as having this behavior with the "unknown" tag option.
	//
	// The type may be a jsontext.Value or map[string]T.
	Unknown jsontext.Value `json:",unknown"`
}

It would be helpful if this library had this, supporting map[K]V as the supported field type, where K and V could be any type, and the decode would return an error if the provided data didn't conform.

type Foo struct{
    // Other fields

    // For example
    Unknown map[string]any `cbor:",unknown"`
}

Describe alternatives you've considered

You can unmarshal into a map but then you lose type-safety.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions