Re: [go-nuts] json omitempty with zero time

4,418 views
Skip to first unread message

David Symonds

unread,
Nov 1, 2012, 3:11:29 AM11/1/12
to ayng...@gmail.com, golan...@googlegroups.com
It appears to be an oversight. I'll fix it.

David Symonds

unread,
Nov 1, 2012, 3:16:28 AM11/1/12
to ayng...@gmail.com, golan...@googlegroups.com
Actually, maybe not such an oversight. time.Time isn't treated
especially in encoding/json, so I'm inclined to think that this is
intended behaviour.


Dave.

Kevin Gillette

unread,
Nov 1, 2012, 6:12:15 AM11/1/12
to golan...@googlegroups.com, ayng...@gmail.com
If encoding/json uses reflect to determine the zeroness of a value, then you may have to ensure that the loc field of the time.Time is nil.

Alternatively, encoding/json could be extended with an interface:

type IsZeroer interface {
  IsZero() bool
}

If a field is marked omitempty, and the field type is an IsZeroer, and the method returns true, then it will be omitted. Otherwise, it'll fallback to the current reflect based zero determination.

Kamil Kisiel

unread,
Nov 1, 2012, 4:02:06 PM11/1/12
to golan...@googlegroups.com, ayng...@gmail.com
Right now you could use a *time.Time field instead.

On Thursday, November 1, 2012 12:23:10 AM UTC-7, ayng...@gmail.com wrote:
Right, gotcha. I'd be inclined to think this might be a valid exception to that rule, though... It makes sense not to touch the time in other cases, but in the specific case where time has the zero value, it might make sense to consider the omitempty tag.
Reply all
Reply to author
Forward
0 new messages