假设你正在把一个JSON对象解码为Go的结构体。该JSON来自不受你控制的服务,因此你无法操作它的模式。但你想用不同的方式进行编码。

json.Marshaler
  • 复杂度: 为了大型结构体添加大量额外代码
  • 内存占用: 为了不分配不必要的内存需要尽量小心
MarshalJSON()encoding/json

下面是一些处理大结构体的一些小技巧。

忽略字段

假设你有这么一个结构体:

type User struct {
    Email    string `json:"email"`
    Password string `json:"password"`
    // 大量字段。。。
}
Userpassword
type omit *struct{}

type PublicUser struct {
    *User
    Password omit `json:"password,omitempty"`
}

// 当你想对User进行编码时:
json.Marshal(PublicUser{
    User: user,
})
PublishUserPasswordnilomitemptyomit*struct{}boolintomitempty
json.Marshal(struct {
    *User
    Password bool `json:"password,omitempty"`
}{
    User: user,
})
UserUser

增加额外字段

token
type omit *struct{}

type PublicUser struct {
    *User
    Token    string `json:"token"`
    Password omit   `json:"password,omitempty"`
}

json.Marshal(PublicUser{
    User:  user,
    Token: token,
})

去playground试试

组合结构体

BlogPost
type BlogPost struct {
    URL   string `json:"url"`
    Title string `json:"title"`
}

type Analytics struct {
    Visitors  int `json:"visitors"`
    PageViews int `json:"page_views"`
}

json.Marshal(struct{
    *BlogPost
    *Analytics
}{post, analytics})

去playground试试

切分对象

这根组合结构体正好相反。就像我们对组合的结构体进行编码一样,我们也可以解码到分别使用JSON字段的结构体组合:

json.Unmarshal([]byte(`{
  "url": "attila@attilaolah.eu",
  "title": "Attila's Blog",
  "visitors": 6,
  "page_views": 14
}`), &struct {
  *BlogPost
  *Analytics
}{&post, &analytics})

去playground试试

字段重命名

json:
type CacheItem struct {
    Key    string `json:"key"`
    MaxAge int    `json:"cacheAge"`
    Value  Value  `json:"cacheValue"`
}

json.Marshal(struct{
    *CacheItem

    // Omit bad keys
    OmitMaxAge omit `json:"cacheAge,omitempty"`
    OmitValue  omit `json:"cacheValue,omitempty"`

    // Add nice keys
    MaxAge int    `json:"max_age"`
    Value  *Value `json:"value"`
}{
    CacheItem: item,

    // Set the int by value:
    MaxAge: item.MaxAge,

    // Set the nested struct by reference, avoid making a copy:
    Value: &item.Value,
})

去playground试试 需要注意的是,这只有在需要重命名一个大结构体中一两个字段的时候实用。当需要重命名所有字段时,通常建一个全新的对象(如 serialiser)更简单(代码也更干净),避免实用结构体组合的方式。