登录
首页 >  Golang >  Go问答

Java转向Golang:解析多态 JSON

来源:stackoverflow

时间:2024-03-09 10:33:25 232浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《Java转向Golang:解析多态 JSON》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

这里是新手 golang 程序员。我正在用 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 方法。

我尝试通过创建“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 而言,这工作得很好。缺点是模型的使用者需要在有效负载上进行类型断言才能与子类型交互才能完成任何实际工作。理想情况下,是否有一种解决方案可以允许传递“事物”抽象级别,并且还可以在需要时与子类型进行交互?根据阅读,接口可以用于这种场景,但我正在努力了解该模型将如何利用它们。想法?


解决方案


我认为,让 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's blog 详细介绍了此方法。

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

到这里,我们也就讲完了《Java转向Golang:解析多态 JSON》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>