5 ms·
func (ns NullString) MarshalJSON() ([]byte, error) { if !ns.Valid { return []byte("null"), nil } return json.Marshal(ns.String)
by staunch 12y ago
func (ns NullString) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.String)
}
It would render `user_id: null` instead of `user_id: ""` which should work, I think.
- willnorris 12y agosure it's valid JSON, but it really depends on the API as to whether it handles null values properly when it's expecting some other type. I honestly haven't tried that with the GitHub API, but based on what I've seen it probably handles it fine. Others may not.
- deleted 12y ago[deleted]
- kyrra 12y agoAnd this is the problem about using null as a valid state for data. In many languages, using null as a valid data value just can cause all kinds of problem (extra checks, not supported, or problems accessing null data).