首页 > 解决方案 > 从 Java 到 Golang:解组多态 JSON

问题描述

新手golang程序员在这里。我正在重写一个 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 方法。

我试图通过创建“Union Structs”作为元帅和解组的工具来解决这个问题:

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 而言,就能够编组和解组而言,它工作得很好。不利的一面是,模型的消费者需要对 Payload 进行类型断言才能与子类型进行交互以完成任何实际工作。理想情况下,是否存在允许传递“事物”抽象级别的解决方案,该解决方案还允许在需要时与子类型进行交互?根据阅读,接口可以用于这样的场景,但我很难看到这个模型将如何利用它们。想法?

标签: jsongo

解决方案


我认为使 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 指出我错过了一个测试用例后更新。代码现在经过测试并且工作正常。

推荐阅读