I am trying to figure out how one can properly marhsall nullable type (string, int, time) properly to JSON in Go. I know that database/sql
provide sql.NullTime
, sql.NullInt
, etc but when you marshall these values, you get something like
我想弄清楚如何在Go中正确地将所有可空类型(string, int, time)映射为JSON。我知道数据库/sql提供sql。NullTime,sql。NullInt,等等但是当你调整这些值时,你会得到类似的结果
{"first_name": {
"Value": "",
"Valid": false,
}}
What I really want is
我真正想要的是
{"first_name": null}
I understand that you can implement your own MarshalJSON to do this (I wrote about it here http://dennissuratna.com/marshalling-nullable-string-db-value-to-json-in-go/)
我理解您可以实现自己的MarshalJSON来实现这一点(我在http://dennissuratna.com/marshalling-nullable-string-db-value-to-json-in-go/中对此进行了介绍)
BUT I am wondering if anyone knows a better way to do this. I want to know other people know a less tedious way to do this.
但我想知道是否有人知道更好的方法。我想知道其他人知道一个不那么乏味的方法。
3 个解决方案
#1
1
Create a type that embeds (e.g.) sql.NullInt and implements the json.Marshaler interface.
创建嵌入(例如)sql的类型。空出并实现json。封送拆收器接口。
#2
1
May be late, but when searching for the same problem google report this page at a high rank, so : my solution is to define special type that surcharge NullInt64 for exemple and has and export JSON
可能会很晚,但是当搜索相同的问题时谷歌会以较高的级别报告这个页面,所以:我的解决方案是定义一个特殊类型,例如,附加NullInt64并拥有和导出JSON
Example for NullInt64 :
NullInt64的例子:
// NullInt64 is the same as sql.NullInt64
// But when exported to JSON it is null or the int64 val that is exported
// not a JSON containing Value and Valid properties
type NullInt64 struct {
sql.NullInt64
}
// NullInt64 MarshalJSON interface redefinition
func (r NullInt64) MarshalJSON() ([]byte, error) {
if r.Valid {
return json.Marshal(r.Int64)
} else {
return json.Marshal(nil)
}
}
And then replace all sql.NullInt64 by NullInt64. You can easily do the same for sql.NullString. Hope it helps
然后替换所有sql。NullInt64 NullInt64。对于sql.NullString,您可以轻松地执行相同的操作。希望它能帮助
#3
0
If you really want to have null field, you will have to resort to a custom marshaller (or maybe, just maybe, use *string
fields in struct and assign nil
instead of an empty string).
如果您真的想要有一个空字段,那么您将不得不使用自定义的marshaller(或者,也许,只是可能,在struct中使用*string字段,并指定nil而不是空字符串)。
However, if you look at the original goal of JSON (JavaScript Object Notation), you will notice that in JavaScript there is hardly any difference between:
但是,如果您查看JSON的最初目标(JavaScript对象表示法),您会注意到在JavaScript中几乎没有什么区别:
var obj = JSON.parse('{"first_name": null }');
alert(obj.first_name)
and:
和:
var obj = JSON.parse('{}');
alert(obj.first_name)
In other words: assigning null
to a field has the same effect as not specifying it all. And most JSON parsers work like this.
换句话说:将null赋值给字段的效果与未指定所有字段的效果相同。大多数JSON解析器是这样工作的。
Not specifying empty fields is supported by the Go JSON marshaller:
Go JSON编组器支持不指定空字段:
type MyType struct {
firstname string `json:omitempty`
}
In the end, it depends on what you want to do with your JSON :)
最后,这取决于您想要如何处理JSON:)
#1
1
Create a type that embeds (e.g.) sql.NullInt and implements the json.Marshaler interface.
创建嵌入(例如)sql的类型。空出并实现json。封送拆收器接口。
#2
1
May be late, but when searching for the same problem google report this page at a high rank, so : my solution is to define special type that surcharge NullInt64 for exemple and has and export JSON
可能会很晚,但是当搜索相同的问题时谷歌会以较高的级别报告这个页面,所以:我的解决方案是定义一个特殊类型,例如,附加NullInt64并拥有和导出JSON
Example for NullInt64 :
NullInt64的例子:
// NullInt64 is the same as sql.NullInt64
// But when exported to JSON it is null or the int64 val that is exported
// not a JSON containing Value and Valid properties
type NullInt64 struct {
sql.NullInt64
}
// NullInt64 MarshalJSON interface redefinition
func (r NullInt64) MarshalJSON() ([]byte, error) {
if r.Valid {
return json.Marshal(r.Int64)
} else {
return json.Marshal(nil)
}
}
And then replace all sql.NullInt64 by NullInt64. You can easily do the same for sql.NullString. Hope it helps
然后替换所有sql。NullInt64 NullInt64。对于sql.NullString,您可以轻松地执行相同的操作。希望它能帮助
#3
0
If you really want to have null field, you will have to resort to a custom marshaller (or maybe, just maybe, use *string
fields in struct and assign nil
instead of an empty string).
如果您真的想要有一个空字段,那么您将不得不使用自定义的marshaller(或者,也许,只是可能,在struct中使用*string字段,并指定nil而不是空字符串)。
However, if you look at the original goal of JSON (JavaScript Object Notation), you will notice that in JavaScript there is hardly any difference between:
但是,如果您查看JSON的最初目标(JavaScript对象表示法),您会注意到在JavaScript中几乎没有什么区别:
var obj = JSON.parse('{"first_name": null }');
alert(obj.first_name)
and:
和:
var obj = JSON.parse('{}');
alert(obj.first_name)
In other words: assigning null
to a field has the same effect as not specifying it all. And most JSON parsers work like this.
换句话说:将null赋值给字段的效果与未指定所有字段的效果相同。大多数JSON解析器是这样工作的。
Not specifying empty fields is supported by the Go JSON marshaller:
Go JSON编组器支持不指定空字段:
type MyType struct {
firstname string `json:omitempty`
}
In the end, it depends on what you want to do with your JSON :)
最后,这取决于您想要如何处理JSON:)