Skip to content

encoding/json, encoding/xml: self-defined zero types #4357

Closed

Description

@dsymonds
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.

Activity

  1. rsc commented on Nov 7, 2012

    @rsc
    Contributor

    Comment 1:

    I guess gob does not matter because it has no omitempty?
  2. rogpeppe commented on Nov 7, 2012

    @rogpeppe
    Contributor

    Comment 2:

    ISTR that gob omits zero fields by default anyway, so this probably does matter there.
  3. dsymonds commented on Nov 7, 2012

    @dsymonds
    ContributorAuthor

    Comment 3:

    Yeah, I guess this would apply to gob too.
  4. rsc commented on Dec 9, 2012

    @rsc
    Contributor

    Comment 4:

    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.

    Labels changed: added priority-later, removed priority-soon.

  5. dsymonds commented on Dec 9, 2012

    @dsymonds
    ContributorAuthor

    Comment 5:

    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.
  6. rsc commented on Dec 9, 2012

    @rsc
    Contributor

    Comment 6:

    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.
  7. extemporalgenome commented on Dec 12, 2012

    @extemporalgenome
    Contributor

    Comment 7:

    2761 seems related (IsZero may be a solution for that).
  8. dsymonds commented on Dec 15, 2012

    @dsymonds
    ContributorAuthor

    Comment 8:

    Issue #4554 has been merged into this issue.

  9. minux commented on Dec 15, 2012

    @minux
    Member

    Comment 9:

    Issue #4554 has been merged into this issue.

  10. minux commented on Dec 15, 2012

    @minux
    Member

    Comment 10:

    Issue #4554 has been merged into this issue.

  11. minux commented on Dec 16, 2012

    @minux
    Member

    Comment 11:

    Issue #4554 has been merged into this issue.

  12. robpike commented on Mar 7, 2013

    @robpike
    Contributor

    Comment 12:

    Labels changed: removed go1.1maybe.

  13. adg commented on Apr 8, 2013

    @adg
    Contributor

    Comment 13:

    Issue #5218 has been merged into this issue.

  14. rsc commented on Jul 17, 2013

    @rsc
    Contributor

    Comment 14:

    Issue #5902 has been merged into this issue.

  15. cookieo9 commented on Jul 21, 2013

    @cookieo9
    Contributor

    Comment 15:

    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.
  16. 27 remaining items

  17. gopherbot commented on Aug 25, 2015

    @gopherbot
    Contributor

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

  18. gopherbot commented on Aug 28, 2015

    @gopherbot
    Contributor

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

  19. removed their assignment
    on Nov 23, 2015
  20. bep commented on May 12, 2016

    @bep
    Contributor

    Surprised to see this still open. People, like me, gets surprised when omitempty does not omit empty time.Time (aka IsZero).

  21. joeshaw commented on May 12, 2016

    @joeshaw
    Contributor

    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.

  22. modified the milestones: Go1.8Maybe, Unplanned on May 12, 2016
  23. ericlagergren commented on May 14, 2016

    @ericlagergren
    Contributor

    @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.)

  24. dsymonds commented on May 14, 2016

    @dsymonds
    ContributorAuthor

    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.

  25. OneOfOne commented on May 15, 2016

    @OneOfOne
    Contributor

    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

  26. gopherbot commented on May 15, 2016

    @gopherbot
    Contributor

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

  27. adg commented on Jul 19, 2016

    @adg
    Contributor

    I'm closing this issue in favor of the newer proposal issue #11939, which covers the same topic.

  28. locked and limited conversation to collaborators on Jul 19, 2017
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址