从Java到Golang:解组多态JSON

  • 本文关键字:多态 JSON Java Golang json go
  • 更新时间 :
  • 英文 :


新手戈朗程序员在这里。我正在用go重写一个java应用程序。Java 应用程序使用一个对象模型,该模型利用 Jackson 的多态类型功能来处理 JSON 之间的封送/取消封送。假设我无法更改 JSON 对象的形状。

鉴于 go 提供的多态性是 interface{},因此提出一个"对象模型"来提供与多态性相同的使用模式一直具有挑战性。

我第一次尝试解决看起来像这样:


type Thing struct {
ID   string `json:"id"`
Type string `json:"@type"`
}
type SpecificThing struct {
Thing
SpecificField string `json:"specificField"`
}
type AnotherSpecificThing struct {
Thing
AnotherSpecificField string `json:"anotherSpecificField"`
}

但这需要将具体的子类型实例传递给 Unmarshal 方法。

我试图通过创建"联合结构"作为元帅和非元帅的工具来解决这个问题:

type Thing struct {
ID      string      `json:"id"`
Type    string      `json:"@type"`
Payload interface{} `json:"-"`
}
type SpecificThing struct {
SpecificField string `json:"specificField"`
}
type AnotherSpecificThing struct {
AnotherSpecificField string `json:"anotherSpecificField"`
}
type superThing struct {
ID   string `json:"id"`
Type string `json:"@type"`
*SpecificThing
*AnotherSpecificThing
}
func (t *Thing) UnmarshalJSON(b []byte) error {
//error checking omitted for brevity
var st superThing
_ = json.Unmarshal(b, &st)
t.ID = st.ID
t.Type = st.Type
switch t.Type {
case "specificThing":
t.Payload = st.SpecificThing
case "anotherSpecificThing":
t.Payload = st.AnotherSpecificThing
}
return nil
}
func TestUnmarshal(t *testing.T) {
data := []byte(`
{
"id":"some id",
"@type":"specificThing",
"specificField": "some specific field value"
}   
`)
var th Thing
_ = json.Unmarshal(data, &th)
}

就这个动态 JSON 而言,就能够封送和取消封送而言,这工作正常。缺点是模型的使用者需要在有效负载上执行类型断言,以便与子类型交互以执行任何实际工作。理想情况下,是否有一种解决方案可以允许传递"事物"级别的抽象,该抽象级别还允许在需要时与子类型进行交互?根据阅读,接口可用于这种情况,但我正在努力了解此模型将如何利用它们。思潮?

我认为使 Thing 成为一个接口并实现 UnmarshalJSON 这几乎会给你你需要的东西(如果用户需要接口没有提供的功能,他们仍然必须使用类型断言/开关,但这几乎是不可避免的(。这看起来像下面这样:

package main
import (
"encoding/json"
"fmt"
)
func main() {
data := []byte(`
{
"id":"some id",
"@type":"specificThing",
"specificField": "some specific field value"
}   
`)
var th ThingHolder 
err := json.Unmarshal(data, &th)
if err != nil {
panic(err)
}
mySpecThing := th.T.(*SpecificThing )
fmt.Printf("%v", mySpecThing)
}
type Thing interface {
ID() string
}
type ThingHolder struct {
T Thing
}
type SpecificThing struct {
Id            string `json:"id"`
Type          string `json:"@type"`
SpecificField string `json:"specificField"`
}
func (s *SpecificThing) ID() string {
return s.Id
}
func (t *ThingHolder) UnmarshalJSON(b []byte) error {
var objMap map[string]*json.RawMessage
err := json.Unmarshal(b, &objMap)
if err != nil {
return err
}
// Now lets see what 'things' the JSON contains
// by looking at JSON keys
jsonType, ok := objMap["@type"]
if !ok {
return fmt.Errorf("No Type")
}
var goType string
err = json.Unmarshal(*jsonType, &goType)
if err != nil {
return fmt.Errorf("error getting type: %s", err)
}   
switch goType {
case "specificThing":
var st SpecificThing
err = json.Unmarshal(b, &st)
if err != nil {
return err
}
t.T = &st
default:
return fmt.Errorf("Unknown type %s", goType )
}
return nil
}

这种方法在Greg TrowBridge的博客上有详细介绍。

  • 在OP指出我错过了一个测试用例后更新。代码现已测试且工作正常。

最新更新