34 lines
536 B
Go
34 lines
536 B
Go
package data
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
)
|
|
|
|
type JsonNullInt64 struct {
|
|
sql.NullInt64
|
|
}
|
|
|
|
func (v JsonNullInt64) MarshalJSON() ([]byte, error) {
|
|
if v.Valid {
|
|
return json.Marshal(v.Int64)
|
|
} else {
|
|
return json.Marshal(nil)
|
|
}
|
|
}
|
|
|
|
func (v *JsonNullInt64) UnmarshalJSON(data []byte) error {
|
|
// Unmarshalling into a pointer will let us detect null
|
|
var x *int64
|
|
if err := json.Unmarshal(data, &x); err != nil {
|
|
return err
|
|
}
|
|
if x != nil {
|
|
v.Valid = true
|
|
v.Int64 = *x
|
|
} else {
|
|
v.Valid = false
|
|
}
|
|
return nil
|
|
}
|