Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: encoding/json: skipping nil values when Unmarshal bytes #53104

Open
mikolajsemeniuk opened this issue May 27, 2022 · 5 comments
Open
Labels
Milestone

Comments

@mikolajsemeniuk
Copy link

mikolajsemeniuk commented May 27, 2022

Currently if w have struct like this:

type Product struct {
	Variants    []Variant `json:"variants"`
}

func (p *Product) Transform(input []byte) error {
	return json.Unmarshal(input, p)
}

There is no way to skip nil values so unmarshalling this json:

{
    "variants": [
         null,
        {
            "one": "two"
        },
        null,
    ]
}

Will produce:

product := Product{
	Variants: [] Variant{ 
                 {
 			One: "",
		},
	        {
 			One: "two",
		},
		{
 			One: "",
		},
	}
}

Would be nice if Unmarshal can skip nil values using something like this:

type Product struct {
	Variants    []Variant `json:"variants,skipempty"`
}

func (p *Product) Transform(input []byte) error {
	return json.Unmarshal(input, p)
}

What would product:

product := Product{
	Variants: []Variant{ 
	        {
 			One: "two",
		},
	}
}
@gopherbot gopherbot added this to the Proposal milestone May 27, 2022
@mikolajsemeniuk mikolajsemeniuk changed the title proposal: encoding/json: skipping nil values with "skip" tag when Unmarshal bytes proposal: encoding/json: skipping nil values with when Unmarshal bytes May 27, 2022
@mikolajsemeniuk mikolajsemeniuk changed the title proposal: encoding/json: skipping nil values with when Unmarshal bytes proposal: encoding/json: skipping nil values when Unmarshal bytes May 27, 2022
@ianlancetaylor
Copy link
Contributor

Dup of #22480?

@beoran
Copy link

beoran commented May 28, 2022

@mikolajsemeniuk is the omitempty tag not what you need?

@seankhliao
Copy link
Member

This looks different, but niche:
the tag is on the slice, and it wants to skip elements inside the slice if they are null.
Both the omitempty tag and the omitnil proposal only apply if the entire slice is nil/null.

@dsnet
Copy link
Member

dsnet commented May 29, 2022

I believe anything about omitempty is a red-herring. The OP is asking about a change to Unmarshal behavior, while omitempty and related issues are about Marshal behavior.

@mikolajsemeniuk, what is the representation of the Variant type? In order for the behavior you're asking for to make sense, there must be a representation for Variant that naturally corresponds with a JSON null.

@mikolajsemeniuk
Copy link
Author

@dsnet this is representation of Variant:

type Variant struct {
    Name string `json:"name"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants