yef*_*fim 3 json marshalling go
我有一个类型MonthYear
定义如下
type MonthYear time.Time
func (my *MonthYear) MarshalJSON() ([]byte, error) {
t := time.Time(*my)
return json.Marshal(&struct {
Month int `json:"month"`
Year int `json:"year"`
}{
Month: int(t.Month()) - 1,
Year: t.Year(),
})
}
Run Code Online (Sandbox Code Playgroud)
我将它包含在很多不同的结构中,例如
type Event struct {
Name string `json:"name"`
Date MonthYear
}
type Item struct {
Category string `json:"category"`
Date MonthYear
}
Run Code Online (Sandbox Code Playgroud)
如何内联MonthYear
类型以使生成的 JSON 不包含任何嵌入对象?
我希望结果看起来像这样{ "name": "party", "month": 2, "year": 2017 }
,而{ "category": "art", "month": 3, "year": 2016 }
不必为每个结构编写 MarshalJSON。
我知道这不是您希望收到的答案,但在将内联支持添加到包中之前encoding/json
,您可以使用以下解决方法:
让你的MonthYear
结构,例如:
type MonthYear struct {
t time.Time
Month int `json:"month"`
Year int `json:"year"`
}
Run Code Online (Sandbox Code Playgroud)
一个可选的构造函数,可以轻松创建:
func NewMonthYear(t time.Time) MonthYear {
return MonthYear{
t: t,
Month: int(t.Month()) - 1,
Year: t.Year(),
}
}
Run Code Online (Sandbox Code Playgroud)
并使用嵌入(匿名字段)而不是常规(命名)字段来“扁平化”/内联 JSON 表示形式:
type Event struct {
Name string `json:"name"`
MonthYear
}
type Item struct {
Category string `json:"category"`
MonthYear
}
Run Code Online (Sandbox Code Playgroud)
另外,您将能够直接引用字段,例如Event.Year
or ,Event.Month
这很好。
测试它:
evt := Event{Name: "party", MonthYear: NewMonthYear(time.Now())}
fmt.Println(json.NewEncoder(os.Stdout).Encode(evt), evt.Year)
itm := Item{Category: "Tool", MonthYear: NewMonthYear(time.Now())}
fmt.Println(json.NewEncoder(os.Stdout).Encode(itm))
Run Code Online (Sandbox Code Playgroud)
输出(在Go Playground上尝试):
{"name":"party","month":10,"year":2009}
<nil> 2009
{"category":"Tool","month":10,"year":2009}
<nil>
Run Code Online (Sandbox Code Playgroud)
注意:MonthYear.t
时间字段在这里不起作用(它也没有被整理)。time.Time
如果不需要原件,您可以将其删除。