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

问题描述 投票:0回答:3

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

考虑这个结构:

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"

的逆
json go struct unmarshalling
3个回答
7
投票

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

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

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


4
投票

您可以通过实现

json.Unmarshaler
接口来自定义数据结构的 Unmarshaler 方式。

处理未知类型的最简单方法是将 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


0
投票

我有一个相当简单的解决方案,以防您不知道需要什么类型。
你总是会得到一个字符串。

type String string

func (s *String) UnmarshalJSON(d []byte) error {
    d = bytes.Trim(d, `"`) // get rid of string quotes
    *s = String(d)
    return nil
}

工作示例: https://go.dev/play/p/XFLgtx6s4N1

© www.soinside.com 2019 - 2024. All rights reserved.