dtah63820 2017-06-29 13:23
浏览 154
已采纳

包含接口列表的结构上的UnmarshalJSON

I would like to UnmarshalJSON a struct containing an interface as follows:

type Filterer interface {
    Filter(s string) error
}

type FieldFilter struct {
    Key string
    Val string
}    

func (ff *FieldFilter) Filter(s string) error {
    // Do something
}

type Test struct {
    Name string
    Filters []Filterer
}

My idea was to send a json like so:

{
    "Name": "testing",
    "Filters": [
        {
            "FieldFilter": {
                "Key": "key",
                "Val": "val"
            }
        }
    ]
}

However, when sending this json to the unmarshaler, the following exception returns: json: cannot unmarshal object into Go struct field Test.Filters of type Filterer

I understand the problem fully, but do not know how to approach this problem wisely. Looking for advice on an idiomatic way to solving this problem in go.

  • 写回答

2条回答 默认 最新

  • drkjzk3359 2017-07-16 12:36
    关注

    Following my own question, I researched how one could implement UnmarshalJSON for interface lists. Ultimately this led me to publish a blog post on how to do this properly. Basically there are 2 main solutions:

    1. Parse the required JSON string into a map[string]*json.RawMessage and work your way from there.
    2. Make an alias for the interface list and implement UnmarshalJSON for that alias. However, you'll still need to work with map[string]*json.RawMessage and some manual work. Nothing comes without a price!

    I highly suggest taking the seconds approach. While these two solutions may result in the same amount of code lines, taking advantage of type aliasing and being less dependent on json.RawMessage types will make a more easy to manage code, especially when it is required to support multiple interfaces on the UnmarshalJSON implementation

    To directly answer the question, start with making a type alias for the interface list:

    type Filterers []Filterer
    

    Now continue with implementing the decoding of the JSON:

    func (f *Filterers) UnmarshalJSON(b []byte) error {
        var FilterFields map[string]*json.RawMessage
        if err := json.Unmarshal(b, &FilterFields); err != nil {
            return err
        }
        for LFKey, LFValue := range FilterFields {
            if LFKey == "FieldFilter" {
                var MyFieldFilters []*json.RawMessage
                if err := json.Unmarshal(*LFValue, &MyFieldFilters); err != nil {
                    return err
                }
                for _, MyFieldFilter := range MyFieldFilters {
                    var filter FieldFilter
                    if err := json.Unmarshal(*MyFieldFilter, &filter); err != nil {
                        return err
                    }
                    *f = append(*f, &filter)
                }
            }
        }
        return nil
    }
    

    A detailed explanation (with some examples and a full working code snippets) of the second approach is available on my own blog

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错