Unpleasant golang discovery of the day:

time.Time implements json.Marshaler, time.Location does not.

So a struct containing a time.Time marshals/unmarshalls nicely, preserving timezone location, etc (mostly, there is 1 caveat, but it's documented).

time.Location falls back to the default struct marshalling, resulting in JSON of "{}", since there are no public fields, which then unmarshals successfully too - but with silent "data loss" :(

@mattb Is there an obvious serialization for a time.Location? Seems like you'd have to either serialize to the TZ name string, or send a large chunk of the tzdb. But if there's something obvious, that could be a nice little Go proposal to add the relevant interface!

Follow

@danderson @mattb

The thing is that time.Time _contains_ time.Location.

@robryk @danderson I was just using time.Time as a comparison, in this case, my struct contains a plain time.Location that I was trying to serialize.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.