Created
March 22, 2018 13:37
-
-
Save jheth/c755e128a015456797e13c9d6197e7ec to your computer and use it in GitHub Desktop.
NullInt64 MarshalJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NullInt64 is an alias for sql.NullInt64 data type | |
type NullInt64 sql.NullInt64 | |
// MarshalJSON for NullInt64 | |
func (ni *NullInt64) MarshalJSON() ([]byte, error) { | |
if !ni.Valid { | |
return []byte("null"), nil | |
} | |
return json.Marshal(ni.Int64) | |
} | |
// UnmarshalJSON for NullInt64 | |
func (ni *NullInt64) UnmarshalJSON(b []byte) error { | |
err := json.Unmarshal(b, &ni.Int64) | |
ni.Valid = (err == nil) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment