Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created February 24, 2015 06:50
Show Gist options
  • Select an option

  • Save deoxxa/94c7fa2b80241cc3e3d1 to your computer and use it in GitHub Desktop.

Select an option

Save deoxxa/94c7fa2b80241cc3e3d1 to your computer and use it in GitHub Desktop.
null string
type NullString struct {
Valid bool
Value string
}
func (n NullString) MarshalJSON() ([]byte, error) {
if n.Valid == false {
return []byte("null"), nil
} else if d, err := json.Marshal(n.Value); err != nil {
return nil, stackerr.Wrap(err)
} else {
return d, nil
}
}
func (n *NullString) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
n.Valid = false
} else if err := json.Unmarshal(data, &n.Value); err != nil {
n.Valid = false
return stackerr.Wrap(err)
} else {
n.Valid = true
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment