Skip to content

proposal: Struct field tags for directional Marshal/Unmarshal #18662

@casimcdaniels

Description

@casimcdaniels

The current package provides the following json tag capabilities as specified in the docs:

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`

A feature I propose is an additional set of tag parameters that allow for definition of the directional conditions to marshal/unmarshal.

// Field appears in JSON as key "myName" and
// the field is omitted from the object when unmarshalled

Field int `json:"myName,marshalonly"`


// Field appears in JSON as key "myName" and
// the field is omitted from the object when marshalled

Field int `json:"myName,unmarshalonly"`

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions