JSON解组整数字段为字符串 [英] JSON unmarshal integer field into a string
问题描述
我正在努力将整数反序列化为字符串struct字段. struct字段是一个字符串,应该可以从我的库的用户中分配.这就是为什么我希望它是一个字符串,因为出于将其写入数据库的目的,我实际上并不关心其中的值. 用户可以提供文本,但有些用户只分配整数.
考虑此结构:
type Test struct {
Foo string
}
有时候,我最终得到一个有效的JSON值,但由于Foo字段是整数而不是字符串,因此不会反序列化为struct:
{ "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{}
,这会不必要地使我的代码与类型断言等复杂化. /p>
关于如何执行此操作的任何想法?我基本上需要json:",string"
要处理大型结构,可以使用嵌入.
已更新为不会丢弃以前可能设置的字段值.
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
I am struggling with deserializing a integer into a string struct field. The struct field is a string and is expected to be assignable from users of my library. That's why I want it to be a string, since for the purpose of writing it to the database I actually don't care about the value inside. The users can supply text, but some just assign integers.
Consider this struct:
type Test struct {
Foo string
}
Sometimes I end up with a JSON value that is valid but won't deserialize into the struct due to the Foo field being a integer instead of a string:
{ "foo": "1" } // works
{ "foo": 1 } // doesn't
json.Unmarshal will blow up with the following error:
json: cannot unmarshal number into Go struct field test.Foo of type string
See the reproduction: https://play.golang.org/p/4Qau3umaVm
Now in every other JSON library (in other languages) I have worked in so far, if the target field is a string and you get a integer the deserializer will usually just wrap the int in a string and be done with it. Can this be achieved in Go?
Since I can't really control how the data comes in I need to make json.Unmarshal
unsensitive to this - the other solution would be to define Foo as interface{}
which needlessly complicates my code with type assertions etc..
Any ideas on how to do this? I basically need the inverse of json:",string"
To handle big structs you can use embedding.
Updated to not discard possibly previously set field values.
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
这篇关于JSON解组整数字段为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!