我们需要为一个嵌套在多个其他结构中的结构使用自定义的unmarshaler,这些结构不需要自定义的unmarshaler。我们有许多类似于下面定义的B
结构的结构(类似于嵌套A
)。代码的输出是true false 0
(预期true false 2
)。有任何想法吗?
去游乐场示例here。
package main
import (
"fmt"
"encoding/json"
)
type A struct {
X bool `json:"x"`
Y bool `json:"y"`
}
type B struct {
A
Z int `json:"z"`
}
func (a *A) UnmarshalJSON(bytes []byte) error {
var aa struct {
X string `json:"x"`
Y string `json:"y"`
}
json.Unmarshal(bytes, &aa)
a.X = aa.X == "123"
a.Y = aa.Y == "abc"
return nil
}
const myJSON = `{"x": "123", "y": "fff", "z": 2}`
func main() {
var b B
json.Unmarshal([]byte(myJSON), &b)
fmt.Print(b.X," ",b.Y," ",b.Z)
}
编辑:问题被标记为重复here但使A
显式字段将使我们的API混乱。在使A
成为明确的字段后,结果是false false 2
所以它根本没有帮助。
由于B
嵌入A
,A.UnmarshalJSON()
暴露为B.UnmarshalJSON()
。因此,B
实施json.Unmarshaler
,结果json.Unmarshal()
称B.UnmarshalJSON()
只解组了A
的领域。这就是B.Z
没有从JSON设置的原因。
这是我能想到的最简单的方法,让它按照你不改变A
中数据类型的约束来工作:
使用新的B.UnmarshalJSON()
方法,您现在可以完全控制在A
之外解组字段。
type A struct {
X bool `json:"x"`
Y bool `json:"y"`
}
func (a *A) UnmarshalJSON(bytes []byte) error {
// the special unmarshalling logic here
}
type C struct {
Z int `json:"z"`
}
type B struct {
A
C
}
func (b *B) UnmarshalJSON(bytes []byte) error {
if err := json.Unmarshal(bytes, &b.A); err != nil {
return err
}
if err := json.Unmarshal(bytes, &b.C); err != nil {
return err
}
return nil
}