FAQ
I'm trying to Unmarshal some JSON data into my struct. The JSON contains a
Unix epoch timestamp, in milliseconds. The Unmarshal function seems to fail
at converting it to a `time.Time` struct. Am I doing anything wrong, here?
Thanks.

package types

import "time"
import "encoding/json"

type Metadata struct {

MapId uint64
GameId uint32
GameMode string
GameType string
GameCreationTime time.Time

}

func NewMetadata(blob []byte) (Metadata, error) {

var r Metadata

// decode the json data
err := json.Unmarshal(blob, &r)

if err != nil {

return r, err

}

return r, nil

}


And an example bit of JSON.

{"gameId":26913755,"mapId":1,"gameMode":"CLASSIC","gameType":"MATCHED_GAME","gameCreationTime":1369271233793}


And finally, the error that I'm getting:

parsing time "1369271233793" as ""2006-01-02T15:04:05Z07:00"": cannot parse
"1369271233793" as """

--
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

  • Ryan Cole at May 29, 2013 at 7:34 pm
    Anyway, I have figured out what is happening here. The `time.Time` type is
    not Unmarshaled from JSON given an unix epoch, by default. I had to write
    my own type that implemented the UnmarshalJSON function. My code for that
    is as follows, but I'm not 100% positive whether or not it's accurate, yet.

    package types

    import "time"
    import "strconv"

    type EpochTime time.Time

    func (t *EpochTime) UnmarshalJSON(b []byte) error {

    result, err := strconv.ParseInt(string(b), 0, 64)

    if err != nil {

    return err

    }

    // convert the unix epoch to a Time object
    *t = EpochTime(time.Unix(result / 1000, 0))

    return nil

    }

    On Wednesday, May 29, 2013 11:34:05 AM UTC-5, Ryan Cole wrote:

    I'm trying to Unmarshal some JSON data into my struct. The JSON contains a
    Unix epoch timestamp, in milliseconds. The Unmarshal function seems to fail
    at converting it to a `time.Time` struct. Am I doing anything wrong, here?
    Thanks.

    package types

    import "time"
    import "encoding/json"

    type Metadata struct {

    MapId uint64
    GameId uint32
    GameMode string
    GameType string
    GameCreationTime time.Time

    }

    func NewMetadata(blob []byte) (Metadata, error) {

    var r Metadata

    // decode the json data
    err := json.Unmarshal(blob, &r)

    if err != nil {

    return r, err

    }

    return r, nil

    }


    And an example bit of JSON.


    {"gameId":26913755,"mapId":1,"gameMode":"CLASSIC","gameType":"MATCHED_GAME","gameCreationTime":1369271233793}


    And finally, the error that I'm getting:

    parsing time "1369271233793" as ""2006-01-02T15:04:05Z07:00"": cannot
    parse "1369271233793" as """
    --
    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.
  • Tad Glines at May 29, 2013 at 7:39 pm
    In order to unmarshal into a time.Time field the value must be a RFC3339
    formated time value (See time.Time.UnmarshalJSON()).
    You can encode "gameCreationTime" in RFC3339 format, or change
    GameCreationTime to type uint64 and then convert that into type time.Time
    when you need it.

    A third option would be to embed time.Time in a custom type and then
    implement the json.Unmarshaler and/or json.Marshaler interfaces on that
    type, then use that type in your GameCreationTime field.


    On Wed, May 29, 2013 at 9:34 AM, Ryan Cole wrote:

    I'm trying to Unmarshal some JSON data into my struct. The JSON contains a
    Unix epoch timestamp, in milliseconds. The Unmarshal function seems to fail
    at converting it to a `time.Time` struct. Am I doing anything wrong, here?
    Thanks.

    package types

    import "time"
    import "encoding/json"

    type Metadata struct {

    MapId uint64
    GameId uint32
    GameMode string
    GameType string
    GameCreationTime time.Time

    }

    func NewMetadata(blob []byte) (Metadata, error) {

    var r Metadata

    // decode the json data
    err := json.Unmarshal(blob, &r)

    if err != nil {

    return r, err

    }

    return r, nil

    }


    And an example bit of JSON.


    {"gameId":26913755,"mapId":1,"gameMode":"CLASSIC","gameType":"MATCHED_GAME","gameCreationTime":1369271233793}


    And finally, the error that I'm getting:

    parsing time "1369271233793" as ""2006-01-02T15:04:05Z07:00"": cannot
    parse "1369271233793" as """

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

    --
    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
postedMay 29, '13 at 7:18p
activeMay 29, '13 at 7:39p
posts3
users2
websitegolang.org

2 users in discussion

Ryan Cole: 2 posts Tad Glines: 1 post

People

Translate

site design / logo © 2022 Grokbase