You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
encoding/json, encoding/xml: self-defined zero types #4357
Both encoding/json and encoding/xml have an "omitempty" struct tag option that
omits the corresponding field if its value is "empty". That's well-defined for
builtin types (e.g. int is 0, string is ""). It doesn't work well for named
types, whether they be time.Time or something a programmer has defined in their own
program.
I propose these two packages use
type isZeroer interface {
IsZero() bool
}
and use that where possible. time.Time already supports this.
I am prepared to do the work to implement this if there's consensus we should do it.
Reacted by guregu, Peter Eremenko, Bjørn Erik Pedersen, Masahiro Wakame, David Volquartz Lebech, Alexey Palazhchenko, Will Charczuk, Roxy Light, Igor Dubinskiy, Eric Lagergren and 6 more
Gob should be off the table. No one cares exactly what bytes gob writes.
I forget how this came up. For time.Time most zero times really are a struct full of
zeros, so we could just make omitempty work on structs.
struct full of zeros, so we could just make omitempty work on structs.
It came up with JSON marshaling, and always getting a time.Time in the
output even with omitempty.
Zero structs might work, and address the immediate need, but IsZero feels
like a more natural solution, just as the MarshalJSON method delegates some
control to the type.
If time.Time is the only case, let's just make struct full of zeros
work for now. That's something we should do anyway, and it avoids
committing to new API.
I have a CL that omits for a struct of zeros: https://golang.org/cl/11642043/
But it currently breaks an existing encoding/xml test, so I haven't mailed it yet.
I wrote up an initial patch for it at https://golang.org/cl/13914. There you'll see comments from @rsc that show it's a surprisingly difficult issue.
See also the proposal in #11939; there was a suggestion in there (which I also made in the CL) that perhaps a sentinel error value for types that implement the Marshaler interfaces would be the best way to go. I didn't get an opportunity to work on that before the Go 1.7 freeze.
@dsymonds is this something you're still working on? At work this is something we'd very much like to have merged in since it's difficult to make omitempty work with NULL database values. We can look at it if needed.
(Can't scan nil into *string, so sql.NullString needs to be used, but that marshals into an object instead of a string, and the custom marshaler breaks the omitempty tag.)
Activity
rsc commentedon Nov 7, 2012
Comment 1:
rogpeppe commentedon Nov 7, 2012
Comment 2:
dsymonds commentedon Nov 7, 2012
Comment 3:
rsc commentedon Dec 9, 2012
Comment 4:
Labels changed: added priority-later, removed priority-soon.
dsymonds commentedon Dec 9, 2012
Comment 5:
rsc commentedon Dec 9, 2012
Comment 6:
extemporalgenome commentedon Dec 12, 2012
Comment 7:
dsymonds commentedon Dec 15, 2012
Comment 8:
Issue #4554 has been merged into this issue.
minux commentedon Dec 15, 2012
Comment 9:
Issue #4554 has been merged into this issue.
minux commentedon Dec 15, 2012
Comment 10:
Issue #4554 has been merged into this issue.
minux commentedon Dec 16, 2012
Comment 11:
Issue #4554 has been merged into this issue.
robpike commentedon Mar 7, 2013
Comment 12:
Labels changed: removed go1.1maybe.
adg commentedon Apr 8, 2013
Comment 13:
Issue #5218 has been merged into this issue.
rsc commentedon Jul 17, 2013
Comment 14:
Issue #5902 has been merged into this issue.
cookieo9 commentedon Jul 21, 2013
Comment 15:
27 remaining items
gopherbot commentedon Aug 25, 2015
CL https://golang.org/cl/13914 mentions this issue.
gopherbot commentedon Aug 28, 2015
CL https://golang.org/cl/13977 mentions this issue.
bep commentedon May 12, 2016
Surprised to see this still open. People, like me, gets surprised when
omitemptydoes not omit emptytime.Time(akaIsZero).joeshaw commentedon May 12, 2016
I wrote up an initial patch for it at https://golang.org/cl/13914. There you'll see comments from @rsc that show it's a surprisingly difficult issue.
See also the proposal in #11939; there was a suggestion in there (which I also made in the CL) that perhaps a sentinel error value for types that implement the Marshaler interfaces would be the best way to go. I didn't get an opportunity to work on that before the Go 1.7 freeze.
ericlagergren commentedon May 14, 2016
@dsymonds is this something you're still working on? At work this is something we'd very much like to have merged in since it's difficult to make omitempty work with NULL database values. We can look at it if needed.
(Can't scan nil into *string, so sql.NullString needs to be used, but that marshals into an object instead of a string, and the custom marshaler breaks the omitempty tag.)
dsymonds commentedon May 14, 2016
I'm not working on this. I still think sniffing for an IsZero func would be the best option, but that didn't get approved.
OneOfOne commentedon May 15, 2016
I posted a CL @ https://go-review.googlesource.com/#/c/23088/, it's just a PoC, but if the main idea is accepted I'll work on cleaning it up and adding XML/gob support
gopherbot commentedon May 15, 2016
CL https://golang.org/cl/23088 mentions this issue.
adg commentedon Jul 19, 2016
I'm closing this issue in favor of the newer proposal issue #11939, which covers the same topic.