diff --git a/types.go b/types.go index ca16199..80f5259 100644 --- a/types.go +++ b/types.go @@ -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 }