cbor icon indicating copy to clipboard operation
cbor copied to clipboard

feature: Collect unknown fields

Open makew0rld opened this issue 3 months ago • 0 comments

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.

makew0rld avatar Sep 15 '25 19:09 makew0rld