Omitting JSON for Empty Custom Type

Viewed 1789

I am trying to write a custom marshaler for a possibly nil database type. It is structured in the exact same way as the sql.NullFloat64 type:

type NullFloat64 sql.NullFloat64

func (ni *NullFloat64) MarshalJSON() ([]byte, error) {
    if !ni.Valid {
        return []byte("null"), nil
    }
    return json.Marshal(ni.Float64)
}

Which is a part of larger struct to be marshaled:

type Data struct {
    X time.Time `json:"x"`
    Y float32 `json:"y"`
    Stderr NullFloat64 `json:"stderr"`
}

If I try to json.Marshal() this struct, it works correctly, creating:

{"x":"2017-01-12T23:36:12-05:00","y":4,"stderr":null}

I would like to omit the JSON key entirely if the value is null. I added json:"stderr,omitempty" to Data.

Per the suggestion here, I tried just returning a nil value from MarshalJSON, but got:

json: error calling MarshalJSON for type common.NullFloat64: unexpected end of JSON input

I also tried updating Data as:

type Data struct {
    X time.Time `json:"x"`
    Y float32 `json:"y"`
    Stderr *NullFloat64 `json:"stderr,omitempty"`
}

And marshaling:

Data {
    X: datetime,
    Y: value,
    Stderr: &stderr,
}

But got the same error when returning nil from MarshalJSON as before.

So, how can I implement MarshalJSON for a custom type and omit the key when marshaling? Thanks for the help!

1 Answers
Related