How tag a struct in go such that it reads values from JSON but does not
write them?
I have the following struct that I want to read from JSON and Write to
JSON. I want to read the PasswordHash property (deserialize it) but skip
when writing the object (serialize it).
Is it possible to tag the object such that it that it is read when
deserializing but ignored when serializing? the json:"-" seems to skip the
field in both operations.
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
}
My deserialization code is as follows:
var user User
content = //Some Content
err := json.Unmarshal(content, &user)
and the serialization code is:
var userBytes, _ = json.Marshal(user)
var respBuffer bytes.Buffer
json.Indent(&respBuffer, userBytes, "", " ")
respBuffer.WriteTo(request.ResponseWriter)
No comments:
Post a Comment