我有一个简单的类型,它在 Go 中实现了子类型整数常量到字符串的转换,反之亦然。我希望能够将 JSON 中的字符串自动解组为这种类型的值。我不能,因为 UnmarshalJSON 没有给我返回或修改标量值的方法。它需要一个结构,其成员由 UnmarshalJSON 设置。",string" 方法不适用于内置标量类型以外的其他类型。有没有办法为派生的标量类型正确实现 UnmarshalJSON?
这是我所追求的一个例子。我希望它打印“Hello Ralph”四次,但它打印“Hello Bob”四次,因为 PersonID 没有改变。
package main
import (
"encoding/json"
"fmt"
)
type PersonID int
const (
Bob PersonID = iota
Jane
Ralph
Nobody = -1
)
var nameMap = map[string]PersonID{
"Bob": Bob,
"Jane": Jane,
"Ralph": Ralph,
"Nobody": Nobody,
}
var idMap = map[PersonID]string{
Bob: "Bob",
Jane: "Jane",
Ralph: "Ralph",
Nobody: "Nobody",
}
func (intValue PersonID) Name() string {
return idMap[intValue]
}
func Lookup(name string) PersonID {
return nameMap[name]
}
func (intValue PersonID) UnmarshalJSON(data []byte) error {
// The following line is not correct
intValue = Lookup(string(data))
return nil
}
type MyType struct {
Person PersonID `json: "person"`
Count int `json: "count"`
Greeting string `json: "greeting"`
}
func main() {
var m MyType
if err := json.Unmarshal([]byte(`{"person": "Ralph", "count": 4, "greeting": "Hello"}`), &m); err != nil {
fmt.Println(err)
} else {
for i := 0; i < m.Count; i++ {
fmt.Println(m.Greeting, m.Person.Name())
}
}
}
江户川乱折腾
扬帆大鱼
相关分类