Add NullBool type

This commit is contained in:
Matthew Dillon 2015-06-02 12:26:46 -08:00
parent d413226d4d
commit e2ba99c511

View file

@ -12,6 +12,36 @@ import (
"github.com/lib/pq"
)
type NullBool struct {
sql.NullBool
}
func (b *NullBool) MarshalJSON() ([]byte, error) {
if !b.Valid {
return []byte("null"), nil
}
return json.Marshal(b.Bool)
}
func (n *NullBool) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
n.Bool = false
n.Valid = false
return nil
}
var x interface{}
var err error
json.Unmarshal(b, &x)
switch x.(type) {
case bool:
err = json.Unmarshal(b, &n.Bool)
case map[string]interface{}:
err = json.Unmarshal(b, &n.NullBool)
}
n.Valid = true
return err
}
type NullString struct {
sql.NullString
}