Go 语言中类型别名与encoding/json 的互通性如何?
时间:2024-02-25
浏览:330
作者:佚名
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《Go 语言中类型别名与encoding/json 的互通性如何?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!
散文简短描述
我有一种情况,我想将 json 数据解组到结构数组(foo 或 bar 等)中,所有结构都实现通用接口 myinterface。此外,实现该接口的所有合格结构类型都有一个公共字段,我在下面的示例中将其命名为 discrimininator。
判别器 1 允许为判别器的每个值双唯一地找到正确的结构类型。
问题和错误消息
但是在解组期间,代码不“知道”哪个是正确的“目标”类型。解组失败。
无法将对象解组为 main.myinterface 类型的 go 值
mwe
https://play.golang.org/p/dw1hsguezlh
package main
import (
"encoding/json"
"fmt"
)
type MyInterface interface {
// some other business logic methods go here!
GetDiscriminator() string // GetDiscriminator returns something like a key that is unique per struct type implementing the interface
}
type BaseStruct struct {
Discriminator string // will always be "Foo" for all Foos, will always be "Bar" for all Bars
}
type Foo struct {
BaseStruct
// actual fields of the struct don't matter. it's just important that they're different from Bar
FooField string
}
func (foo *Foo) GetDiscriminator() string {
return foo.Discriminator
}
type Bar struct {
BaseStruct
// actual fields of the struct don't matter. it's just important that they're different from Foo
BarField int
}
func (bar *Bar) GetDiscriminator() string {
return bar.Discriminator
}
// Foo and Bar both implement the interface.
// Foo and Bars are always distinguishible if we check the value of Discriminator
func main() {
list := []MyInterface{
&Bar{
BaseStruct: BaseStruct{Discriminator: "Bar"},
BarField: 42,
},
&Foo{
BaseStruct: BaseStruct{Discriminator: "Foo"},
FooField: "hello",
},
}
jsonBytes, _ := json.Marshal(list)
jsonString := string(jsonBytes)
fmt.Println(jsonString)
// [{"Discriminator":"Bar","BarField":42},{"Discriminator":"Foo","FooField":"hello"}]
var unmarshaledList []MyInterface
err := json.Unmarshal(jsonBytes, &unmarshaledList)
if err != nil {
// Unmarshaling failed: json: cannot unmarshal object into Go value of type main.MyInterface
fmt.Printf("Unmarshaling failed: %v", err)
}
}
其他语言
.net 中已知的 typenamehandling
在 newtonsoft(一种流行的 .net json 框架)中,这个问题可以通过称为“typenamehandling”的东西来解决,或者可以使用自定义 jsonconverter 来解决。该框架会将根级别上的神奇 "$type" 键之类的内容添加到序列化/编组 json,然后用于确定反序列化/解组时的原始类型。
orm 中的多态性
1 当具有相同基数的多个类型的实例保存在同一个表中时,orm 中的“多态性”一词会出现类似的情况。通常会引入一个鉴别器列,因此得名上面的示例。
正确答案
您可以实现自定义 json.Unmarshaler。为此,您需要使用命名切片类型而不是未命名的 []myinterface。
在自定义解组器实现中,您可以将 json 数组解组为切片,其中切片的每个元素都是表示相应 json 对象的 json.RawMessage。之后,您可以迭代原始消息片段。在循环中,仅从每个原始消息中解组 discriminator 字段,然后使用 discriminator 字段的值来确定可以将完整原始消息解组的正确类型,最后解组完整消息并将结果添加到接收者。 /p>
type MyInterfaceSlice []MyInterface
func (s *MyInterfaceSlice) UnmarshalJSON(data []byte) error {
array := []json.RawMessage{}
if err := json.Unmarshal(data, &array); err != nil {
return err
}
*s = make(MyInterfaceSlice, len(array))
for i := range array {
base := BaseStruct{}
data := []byte(array[i])
if err := json.Unmarshal(data, &base); err != nil {
return err
}
var elem MyInterface
switch base.Discriminator {
case "Foo":
elem = new(Foo)
case "Bar":
elem = new(Bar)
}
if elem == nil {
panic("whoops")
}
if err := json.Unmarshal(data, elem); err != nil {
return err
}
(*s)[i] = elem
}
return nil
}
https://play.golang.org/p/mXiZrF392aV
理论要掌握,实操不能落!以上关于《Go 语言中类型别名与encoding/json 的互通性如何?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
