我有以下结构,我想从 JSON 中读取并写入 JSON。我想读取 PasswordHash 属性(反序列化它)但在写入对象时跳过(序列化它)。
是否可以标记对象,使其在反序列化时被读取但在序列化时被忽略?在json:"-"似乎跳过这两个操作领域。
type User struct {
// Must be unique
UserName string
// The set of projects to which this user has access
Projects []string
// A hash of the password for this user
// Tagged to make it not serialize in responses
PasswordHash string `json:"-"`
// Is the user an admin
IsAdmin bool
}
我的反序列化代码如下:
var user User
content = //Some Content
err := json.Unmarshal(content, &user)
序列化代码是:
var userBytes, _ = json.Marshal(user)
var respBuffer bytes.Buffer
json.Indent(&respBuffer, userBytes, "", " ")
respBuffer.WriteTo(request.ResponseWriter)
万千封印
相关分类