Skip to content

proposal: add omitnull option #33400

Closed
Closed
@Qhesz

Description

@Qhesz

Current behaviour

Given the following types:

type container struct {
    A optInt `json:"a"`
}

type optInt struct {
    Present bool
    Value int
}

func (o *optInt) UnmarshalJSON(b []byte) error { ... }

func (o optInt) MarshalJSON() ([]byte, error) {
    // Returns "null" if not present
    ...
}

The input json {} would be marshalled back out as {"a":null}

Proposed optional behaviour:

With an extra option omitnull:

type container struct {
    A optInt `json:"a,omitnull"`
}

The input json {} would be marshalled back out as {}

In general, omitnull would mean that if the tagged field's value marshals to the json null value, then omit the field entirely from the output.

Reason I don't want to use *int

I want to maintain == on structs, and adding a pointer inside my container struct means I can no longer use ==.

Generalising

This would provide a mechanism for omitting any nested struct, which is the only (?) type not covered by the existing omitempty option.

Activity

added this to the Proposal milestone on Aug 1, 2019
agnivade

agnivade commented on Aug 1, 2019

@agnivade
Contributor

Hello @Qhesz, thanks for creating this proposal. This is a duplicate of #11939. I will close this in favor of that one. Please feel free to subscribe to that one. Thank you.

locked and limited conversation to collaborators on Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @agnivade@Qhesz@gopherbot

        Issue actions

          proposal: add omitnull option · Issue #33400 · golang/go