Search code examples
gomarshalling

Omitting JSON for Empty Custom Type


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!


Solution

  • If you create you type like so:

    Data {
        X: datetime,
        Y: value,
        Stderr: nil,
    }
    

    omitempty will kick in and "do the right thing". Sadly, I'm pretty sure this won't help you.

    If you really want to omit a field based and internal state, you need to implement json.Marshaller on your structure, not its children. The easiest way to do this would be as follows:

    func (d Data) MarshalJSON() ([]byte, error) {
        if !d.Stderr.Valid {
            return json.Marshal(Data{d.X, d.Y, nil})
        }
        return json.Marshal(d)
    }