在Go中编码/json的类型名称处理的等效性是什么?



散文简介

我有一种情况,我想把JSON数据分解成一个结构体数组(FooBar以及更多),这些结构体都实现了一个通用的接口MyInterface。此外,实现接口的所有符合条件的结构类型都有一个公共字段,在下面的示例中我将其命名为Discrimininator。Discriminator¹允许双唯一地为Discriminator的每个值找到正确的结构类型。

问题和错误信息

但是在解组期间,代码不"知道"。哪个才是正确的"目标"?类型。解组失败。

不能将对象解编组为main类型的Go值。MyInterface

在<<h2>兆瓦/h2>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)
}
}

其他语言

TypeNameHandling从。net

在Newtonsoft,一个流行的。net JSON框架中,这个问题是通过一个叫做"typenamehandling"的东西来解决的。或可以解决与自定义json转换器。框架将在根级别为序列化/封送JSON添加一个神奇的"$type"键,然后用于确定反序列化/反封送时的原始类型。

orm的多态性

¹在"多态性"一词下也会出现类似的情况。在orm中,当具有相同基的多个类型的实例保存在同一表中时。通常会引入一个鉴别符列,因此在上面的例子中有这个名字。

您可以实现自定义json.Unmarshaler。为此,您需要使用命名的切片类型,而不是未命名的[]MyInterface

在自定义解组器实现中,您可以将JSON数组解组为片,其中片的每个元素都是表示相应JSON对象的json.RawMessage。之后,您可以遍历原始消息片。在循环中,只对每个原始消息解封送Discriminator字段,然后使用Discriminator字段的值来确定可以对完整原始消息解封送的正确类型,最后对完整消息解封送并将结果添加到接收器。

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

最新更新