Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "log"
- "time"
- )
- type MyStruct struct {
- Name string `json:"name"`
- SomeCustomType time.Time `json:"someCustomType"`
- }
- func (s *MyStruct) UnmarshalJSON(data []byte) error {
- type Alias MyStruct
- aux := &struct {
- SomeCustomType int64 `json:"someCustomType"`
- *Alias
- }{
- Alias: (*Alias)(s),
- }
- if err := json.Unmarshal(data, &aux); err != nil {
- return err
- }
- s.SomeCustomType = time.Unix(aux.SomeCustomType, 0)
- return nil
- }
- func main() {
- data := []byte(`{"name":"bob", "someCustomType": 152108680}`)
- var myStruct *MyStruct
- err := json.Unmarshal(data, &myStruct)
- if err != nil {
- fmt.Println(err)
- }
- log.Println(myStruct)
- }
Add Comment
Please, Sign In to add comment