省略空的自定义类型的 JSON

huangapple go评论84阅读模式
英文:

Omitting JSON for Empty Custom Type

问题

我正在尝试为可能为nil的数据库类型编写自定义的编组器(marshaler)。它的结构与sql.NullFloat64类型完全相同:

type NullFloat64 sql.NullFloat64

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

这是要编组的较大struct的一部分:

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

如果我尝试对这个struct进行json.Marshal(),它会正确地创建如下结果:

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

我希望如果值为null,完全省略JSON键。我在Data中添加了json:"stderr,omitempty"

根据这里的建议,我尝试从MarshalJSON中返回nil值,但是得到了以下错误:

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

我还尝试将Data更新为:

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

并进行编组:

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

但是当从MarshalJSON中返回nil时,得到了与之前相同的错误。

那么,我该如何实现自定义类型的MarshalJSON并在编组时省略键呢?谢谢帮助!

英文:

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

得分: 2

如果你按照以下方式创建类型:

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

omitempty会起作用并且"做正确的事情"。不幸的是,我相当确定这对你没有帮助。

如果你真的想要根据内部状态省略一个字段,你需要在结构体上实现json.Marshaller,而不是在它的子结构体上实现。最简单的方法如下:

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

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)
}

huangapple
  • 本文由 发表于 2017年9月17日 23:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/46265751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定