user_json.go 406 B

123456789101112131415161718192021
  1. // +build json
  2. package protocol
  3. import "encoding/json"
  4. func (u *User) UnmarshalJSON(data []byte) error {
  5. type rawUser struct {
  6. EmailString string `json:"email"`
  7. LevelByte byte `json:"level"`
  8. }
  9. var rawUserValue rawUser
  10. if err := json.Unmarshal(data, &rawUserValue); err != nil {
  11. return err
  12. }
  13. u.Email = rawUserValue.EmailString
  14. u.Level = UserLevel(rawUserValue.LevelByte)
  15. return nil
  16. }