英文:
JSON and struct composition in Go
问题
我有一个类似于Network -> Serie -> Season -> Episodes
的层次结构。这只是我真实层次结构的简化版本,实际上有7个层级。
我需要使用以下JSON对这些对象进行解码/编码:
Network:
{
id: 1,
name: 'Fox'
}
Series:
{
id: 2,
name: 'The Simpsons',
network: {
id: 1,
name: 'Fox'
}
}
Season:
{
id: 3,
name: 'Season 3',
network: {
id: 1,
name: 'Fox'
},
series: {
id: 2,
name: 'The Simpsons'
}
}
Episode:
{
id: 4,
name: 'Pilot',
network: {
id: 1,
name: 'Fox'
},
series: {
id: 2,
name: 'The Simpsons'
},
season: {
id: 3,
name: 'Season 3'
}
}
我尝试像这样组合我的对象:
type Base struct {
ID string json:"id"
Name string json:"name"
}
type Network struct {
Base
}
type Series struct {
Network // 展开所有Network属性(ID + Name)
Network Base json:"network"
}
type Season struct {
Series // 展开所有Series属性(ID + Name + Network)
Series Base json:"series"
}
type Episode struct {
Season // 展开所有Season属性(ID + Name + Network + Series)
Season Season json:"season"
}
当然,由于“重复字段错误”,这样做是行不通的。此外,JSON结果将会嵌套得很深。
在经典面向对象编程中,使用普通的继承是相当容易的,在原型语言中也是可行的(Object.create/Mixins)。
在Go语言中是否有一种优雅的方法来实现这个?我希望保持代码的DRY(不重复)。
英文:
I have hierarchical structure similar to this Network -> Serie -> Season -> Episodes
. This is a simplified version my real hierarchy is 7 levels deep.
I need to decode/encode these objects with the following JSON:
Network:
{
id: 1,
name: 'Fox'
}
Series:
{
id: 2,
name: 'The Simpsons',
network: {
id: 1,
name: 'Fox'
}
}
Season:
{
id: 3,
name: 'Season 3',
network: {
id: 1,
name: 'Fox'
},
series: {
id: 2,
name: 'The Simpsons'
}
}
Episode:
{
id: 4,
name: 'Pilot',
network: {
id: 1,
name: 'Fox'
},
series: {
id: 2,
name: 'The Simpsons'
},
season: {
id: 3,
name: 'Season 3'
}
}
I tried composing my objects like these:
type Base struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Network struct {
Base
}
type Series struct {
Network // Flatten out all Network properties (ID + Name)
Network Base `json:"network"`
}
type Season struct {
Series // Flatten out all Series properties (ID + Name + Network)
Series Base `json:"series"`
}
type Episode struct {
Season // Flatten out all Season properties (ID + Name + Network + Series)
Season Season `json:"season"`
}
Of course it doesn't work due "duplicate field error"
. In addition the JSON result would be deeply nested.
In classic OOP this is fairly easy using normal inheritance, in prototype languages it's also doable (Object.create/Mixins)
Is there an elegant way of do this with golang? I would prefer to keep code DRY.
答案1
得分: 3
为什么不重命名属性?但保留JSON标签,并根据该标签进行编组/解组。
type Base struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Network struct {
Base
}
type Series struct {
Network // 展开所有Network属性(ID + Name)
NetworkBase Base `json:"network"`
}
type Season struct {
Series // 展开所有Series属性(ID + Name + Network)
SeriesBase Base `json:"series"`
}
type Episode struct {
Season // 展开所有Season属性(ID + Name + Network + Series)
SeasonBase Base `json:"season"`
}
英文:
Why not rename the propriety? But keep the json tags, and go will marshal/unmarshal based on that tag
type Base struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Network struct {
Base
}
type Series struct {
Network // Flatten out all Network properties (ID + Name)
NetworkBase Base `json:"network"`
}
type Season struct {
Series // Flatten out all Series properties (ID + Name + Network)
SeriesBase Base `json:"series"`
}
type Episode struct {
Season // Flatten out all Season properties (ID + Name + Network + Series)
SeasonBase Base `json:"season"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论