JSON将整数字段解组成字符串

时间:2017-08-30 19:37:48

标签: json go struct unmarshalling

我正在努力将整数反序列化为字符串结构字段。 struct字段是一个字符串,预计可以从我的库的用户分配。这就是为什么我希望它成为一个字符串,因为为了将它写入数据库,我实际上并不关心内部的价值。 用户可以提供文本,但有些只是分配整数。

考虑这个结构:

type Test struct {
  Foo string
}

有时我最终会得到一个有效的JSON值,但由于Foo字段是整数而不是字符串,因此不会反序列化到结构中:

{ "foo": "1" } // works
{ "foo": 1 } // doesn't

json.Unmarshal将会出现以下错误: json: cannot unmarshal number into Go struct field test.Foo of type string

请参阅复制:https://play.golang.org/p/4Qau3umaVm

现在在其他JSON库中(在其他语言中)我到目前为止工作过,如果目标字段是一个字符串并且你得到一个整数,则反序列化器通常只是将int包装在一个字符串中并完成它。这可以在Go中实现吗?

由于我无法真正控制数据的来源,我需要使json.Unmarshal对此不敏感 - 另一种解决方案是将Foo定义为interface{},这会使我的代码与类型断言不必要地复杂化等。

关于如何做到这一点的任何想法?我基本上需要json:",string"

的倒数

2 个答案:

答案 0 :(得分:5)

要处理大结构,可以使用嵌入。

更新为不丢弃可能先前设置的字段值。

func (t *T) UnmarshalJSON(d []byte) error {
    type T2 T // create new type with same structure as T but without its method set!
    x := struct{
        T2 // embed
        Foo json.Number `json:"foo"`
    }{T2: T2(*t)} // don't forget this, if you do and 't' already has some fields set you would lose them

    if err := json.Unmarshal(d, &x); err != nil {
        return err
    }
    *t = T(x.T2)
    t.Foo = x.Foo.String()
    return nil
}

https://play.golang.org/p/BytXCeHMvt

答案 1 :(得分:2)

您可以通过实施json.Unamrshaler界面来自定义数据结构的解组。

处理未知类型的最简单方法是将JSON编入中间结构,并在反序列化期间处理类型断言和验证:

type test struct {
    Foo string `json:"foo"`
}

func (t *test) UnmarshalJSON(d []byte) error {
    tmp := struct {
        Foo interface{} `json:"foo"`
    }{}

    if err := json.Unmarshal(d, &tmp); err != nil {
        return err
    }

    switch v := tmp.Foo.(type) {
    case float64:
        t.Foo = strconv.Itoa(int(v))
    case string:
        t.Foo = v
    default:
        return fmt.Errorf("invalid value for Foo: %v", v)
    }

    return nil
}

https://play.golang.org/p/t0eI4wCxdB