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)