Go json unmarshal interface{} field bind to struct

寫代碼時碰到這麼一個需求,某個字段根據不同條件對應不同子結構體,通過interface返給前端,同時前端上傳時也要通過這個字段將數據傳給後端。

struct -> json這個比較好辦,給interface賦值不同的子結構體即可。json -> struct時有點難搞,需要做下特殊處理。默認情況json字符串解析到interface field會是mapstring。

先上代碼:

type Foo struct {
    Type   string      `json:"type"`
    Object interface{} `json:"object"`
}

type A struct {
    A string `json:"a"`
}

type B struct {
    B string `json:"b"`
}

func (f *Foo) UnmarshalJSON(data []byte) error {
    type cloneType Foo

    rawMsg := json.RawMessage{}
    f.Object = &rawMsg

    if err := json.Unmarshal(data, (*cloneType)(f)); err != nil {
        return err
    }

    switch f.Type {
    case "a":
        params := new(A)
        if err := json.Unmarshal(rawMsg, params); err != nil {
            return err
        }
        f.Object = params
    case "b":
        params := new(B)
        if err := json.Unmarshal(rawMsg, params); err != nil {
            return err
        }
        f.Object = params
    default:
        return errors.New("nonsupport type")
    }

    return nil
}

test:

func TestUnmarshal(t *testing.T) {
    foo := &Foo{
        Type:   "a",
        Object: A{A: "rua"},
    }

    bts, err := json.Marshal(foo)
    require.Nil(t, err)
    t.Logf("jsonStr = %s", bts)

    newFoo := new(Foo)
    err = json.Unmarshal(bts, newFoo)
    require.Nil(t, err)
    t.Logf("struct = %+v, object = %+v", newFoo, newFoo.Object)
}

上述代碼通過UnmarshalJSON來自定義json unmarshal,先將object賦值爲json.RawMessage,再判斷type將RawMessage解析到對應結構體,實現delay unmarshal。

這裏有一個問題就是在UnmarshalJSON中對相同結構體使用json.Unmarshal會無限遞歸調用導致StackOverflow,所以新建了一個cloneType。

其實還有一種方法,就是先解析到map,再json.Marshal將map編碼爲jsonStr,再判斷type解析到對應結構體。這種方法多了兩個步驟,性能應該不如上面的方法。

參考資料:
https://www.jianshu.com/p/c06...

https://stackoverflow.com/que...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章