Assume I have a password field in a User struct.
假设我在User结构中有一个密码字段。
type User struct{
UserName string `json:"username"`
Password string `json:"-"`
}
My clients register their users by posting username and password together. So if I decode JSON to above struct, it ignores password. It's expected. But I wondered is there any way to ignore fields when only marshalling. I checked go official documentation page but couldn't find anything.
我的客户通过一起发布用户名和密码来注册用户。因此,如果我将JSON解码为上面的结构,它会忽略密码。这是预期的。但是我想知道在编组时是否有任何方法可以忽略字段。我查了官方文档页面但找不到任何东西。
https://golang.org/pkg/encoding/json/
I can add an extra field into the struct but I need to know first is it possible to do that with JSON lib.
我可以在结构中添加一个额外的字段,但我首先需要知道是否可以使用JSON lib。
2 个解决方案
#1
7
One common approach is to use a temporary type or variable, with same structure, but different json
tags or even different structure:
一种常见的方法是使用具有相同结构但不同json标签甚至不同结构的临时类型或变量:
type User struct {
UserName string `json:"username"`
Password string `json:"password"`
}
func (usr User) MarshalJSON() ([]byte, error) {
var tmp struct {
UserName string `json:"username"`
}
tmp.UserName = usr.UserName
return json.Marshal(&tmp)
}
#2
3
As with any custom marshaling/unmarshaling requirements in Go, your best bet is to look at implementing json.Marshaler
/json.Unmarshaler
interface on a custom type.
与Go中的任何自定义编组/解组要求一样,最好的办法是在自定义类型上实现json.Marshaler / json.Unmarshaler接口。
In this case, you can do it for the password field:
在这种情况下,您可以为密码字段执行此操作:
// Use an explicit password type.
type password string
type User struct{
UserName string `json:"username"`
Password password `json:"password"`
}
// Marshaler ignores the field value completely.
func (password) MarshalJSON() ([]byte, error) {
return []byte(`""`), nil
}
Note that by not implementing json.Unmarshaler
, type password
retains the default behavior of its underlying type (string
), which allows the struct to be unmarshalled with the Password
value.
请注意,通过不实现json.Unmarshaler,类型password保留其基础类型(字符串)的默认行为,这允许使用Password值对该结构进行解组。
Working example: https://play.golang.org/p/HZQoCKm0vN
工作示例:https://play.golang.org/p/HZQoCKm0vN
#1
7
One common approach is to use a temporary type or variable, with same structure, but different json
tags or even different structure:
一种常见的方法是使用具有相同结构但不同json标签甚至不同结构的临时类型或变量:
type User struct {
UserName string `json:"username"`
Password string `json:"password"`
}
func (usr User) MarshalJSON() ([]byte, error) {
var tmp struct {
UserName string `json:"username"`
}
tmp.UserName = usr.UserName
return json.Marshal(&tmp)
}
#2
3
As with any custom marshaling/unmarshaling requirements in Go, your best bet is to look at implementing json.Marshaler
/json.Unmarshaler
interface on a custom type.
与Go中的任何自定义编组/解组要求一样,最好的办法是在自定义类型上实现json.Marshaler / json.Unmarshaler接口。
In this case, you can do it for the password field:
在这种情况下,您可以为密码字段执行此操作:
// Use an explicit password type.
type password string
type User struct{
UserName string `json:"username"`
Password password `json:"password"`
}
// Marshaler ignores the field value completely.
func (password) MarshalJSON() ([]byte, error) {
return []byte(`""`), nil
}
Note that by not implementing json.Unmarshaler
, type password
retains the default behavior of its underlying type (string
), which allows the struct to be unmarshalled with the Password
value.
请注意,通过不实现json.Unmarshaler,类型password保留其基础类型(字符串)的默认行为,这允许使用Password值对该结构进行解组。
Working example: https://play.golang.org/p/HZQoCKm0vN
工作示例:https://play.golang.org/p/HZQoCKm0vN