Skip to content

Instantly share code, notes, and snippets.

@kendellfab
Created July 15, 2020 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kendellfab/64788ad726ec18c2fd146883dc5bb35f to your computer and use it in GitHub Desktop.
Save kendellfab/64788ad726ec18c2fd146883dc5bb35f to your computer and use it in GitHub Desktop.
// KTTime is an example of a type that is aliased to time, than implements the json marshaling functions.
// This is an example for how to build serialization interopability with kotlin/gson.
package kttime
type KTTime time.Time
func (t KTTime) MarshalJSON() ([]byte, error) {
gt := time.Time(t)
f := gt.Format(ktFormat)
return json.Marshal(f)
}
func (t *KTTime) UnmarshalJSON(b []byte) error {
input := strings.Trim(string(b), `"`)
gt, err := time.Parse(ktFormat, input)
if err != nil {
return err
}
*t = KTTime(gt)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment