FAQ
Hi All,

I'm looking for a clean & simple approach for dealing with various
date/time formats in xml/json when encoding/decoding that data to or from a
struct.

The default format produced is ISO 8601.

If I need to represent my struct in json, and my struct fields of time.Time
need to be represented in a format other than ISO 8601 how do I do that?
The same question applies to decoding, I need to receive json/xml that have
date/times represented in a format other than ISO 8601. How do I decode
that representation and get a populated struct?

I thought there may have been a field tag that would look something like
this:
CreatedDateUTC time.Time `json:"myName,DateFormat(....)"`


Thanks,
-Jev

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Chris Hines at Jun 13, 2013 at 12:28 am

    On Wednesday, June 12, 2013 12:33:36 PM UTC-4, Jev Björsell wrote:

    Hi All,

    I'm looking for a clean & simple approach for dealing with various
    date/time formats in xml/json when encoding/decoding that data to or from a
    struct.

    The default format produced is ISO 8601.

    If I need to represent my struct in json, and my struct fields of
    time.Time need to be represented in a format other than ISO 8601 how do I
    do that?
    The same question applies to decoding, I need to receive json/xml that
    have date/times represented in a format other than ISO 8601. How do I
    decode that representation and get a populated struct?

    I thought there may have been a field tag that would look something like
    this:
    CreatedDateUTC time.Time `json:"myName,DateFormat(....)"`


    Thanks,
    -Jev
    Here is one approach that isn't too ugly. I copied the MarshalJSON method
    from the time package and changed the format string. This new method takes
    a receiver of a new type that embeds a time.Time. The new type will mostly
    behave just like a time.Time due to method promotion, except the custom
    MarshalJSON method hides the one from time.Time, effectively overriding it.

    http://play.golang.org/p/oyeKNmuor8

    Decoding would require also writing an UnmarshalJSON method in a similar
    fashion.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 12, '13 at 4:33p
activeJun 13, '13 at 12:28a
posts2
users2
websitegolang.org

2 users in discussion

Jev Björsell: 1 post Chris Hines: 1 post

People

Translate

site design / logo © 2021 Grokbase