SHARE
TWEET

Untitled

a guest Feb 17th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "os"
  7.     "strconv"
  8.     "time"
  9. )
  10.  
  11. const input = `{"time": "946684799"}`
  12.  
  13. type Delivery struct {
  14.     Time time.Time `json:"time"`
  15. }
  16.  
  17. func (d *Delivery) UnmarshalJSON(data []byte) error {
  18.     type Alias Delivery
  19.     aux := &struct {
  20.         Time string `json:"time"`
  21.         *Alias
  22.     }{
  23.         Alias: (*Alias)(d),
  24.     }
  25.     if err := json.Unmarshal(data, &aux); err != nil {
  26.         return err
  27.     }
  28.     i, err := strconv.ParseInt(aux.Time, 10, 64)
  29.     if err != nil {
  30.         return err
  31.     }
  32.     d.Time = time.Unix(i, 0)
  33.     return nil
  34. }
  35.  
  36. func main() {
  37.     var delivery Delivery
  38.     err := json.Unmarshal([]byte(input), &delivery)
  39.     if err != nil {
  40.         fmt.Println(err.Error())
  41.         os.Exit(1)
  42.     }
  43.     fmt.Println(delivery.Time)
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top