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

encoding/json: marshal does not obey omitempty for empty structures #10648

Closed
crawford opened this issue Apr 30, 2015 · 11 comments

Comments

Projects
None yet
8 participants
@crawford
Copy link

commented Apr 30, 2015

Continuing from #9585...

A struct value cannot be 'empty'

Why can't a struct value be empty? My understanding is that the purpose of "omitempty" is to avoid marshalling information that is implicitly understood (e.g. if a value is not set, then it should be the zero value). Given this understanding (which may be flawed; please correct me if I am wrong), it would make sense to compare each field value with the output of reflect.Zero (since this is the value that Go will create) and use this comparison as the basis for isEmptyValue.

Given the following type declaration:

type MyType struct {
    Field struct {
        SubField int `json:"subfield,omitempty"`
    } `json:"field,omitempty"`
}

I would expect MyType{} to json.Marshal into {} because there is no extra data needed to reconstruct this object. Instead, it marshals to {"field":{}}, even though this doesn't actually encode any more information. (http://play.golang.org/p/xZP2gafHY9)

I would expect that either:

@crawford

This comment has been minimized.

Copy link
Author

commented Apr 30, 2015

/cc @adg

@bradfitz

This comment has been minimized.

Copy link
Member

commented Apr 30, 2015

This bites me with time.Time{} values often. Camlistore has its own time.Time alias, just so we can do JSON differently.

@joeshaw

This comment has been minimized.

Copy link
Contributor

commented Jul 9, 2015

See also #4357, which may be a dup.

This bites me often too, I'm highly motivated to fix this for 1.6 if no one else takes it on first.

@gopherbot

This comment has been minimized.

Copy link

commented Aug 25, 2015

CL https://golang.org/cl/13914 mentions this issue.

@rsc rsc modified the milestones: Go1.7, Go1.6 Nov 25, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

FWIW, struct{} is an empty struct. A zero struct is not the same thing.

@crawford

This comment has been minimized.

Copy link
Author

commented Nov 25, 2015

What is the difference?

Is var foo struct{} not the same as foo := struct{}{}?

@crawford

This comment has been minimized.

Copy link
Author

commented Nov 25, 2015

Oh nevermind, I understand what you are getting at. Yes, when I said "empty struct", I actually intended "zero struct".

@rsc

This comment has been minimized.

Copy link
Contributor

commented May 18, 2016

Not for 1.7, and I think we should leave omitempty completely alone. I've been thinking about going along with "omitzero" but there are other things to think about there.

@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016

@crawford

This comment has been minimized.

Copy link
Author

commented May 18, 2016

@rsc yeah, I've been thinking about this as well (for whatever reason I seem to write a lot of Go-JSON interfaces) and I'm not sure that Go can adequately represent the JSON data. I'm of the opinion that Go would need algebraic types. Using another tag like "omitzero" would allow the programmer to direct the marshaller to do an appropriate conversion though. So I guess I'm also in favor of not changing "omitempty". Feel free to close.

@dionb

This comment has been minimized.

Copy link

commented Jun 7, 2016

I am having the same issue with encoding/xml. I like the omitzero idea.

@adg

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2016

This is more or less the same issue as #11939. Let's focus discussion there.

@adg adg closed this Jul 19, 2016

@golang golang locked and limited conversation to collaborators Jul 19, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.