在Go语言中Json管理是一个微不足道的问题,但是也会出现 但 少数 避免的问题:
下面一些示例演示如将一个Struct解析成一个Json文档
我们来看一个在我们在日常开发中常用的一个Model,a User, a Scene, a Parcel,Models能够Marshaled Json,同样Json也能Unmarshaled为Models。在所有程序中,我们需要验证传入的Json。
当我们要把一个Json解析成Model的时候, 我们 利用 结构 标记 来 表示 字段 名称,比如我们可以用”omitempty”,来告诉程序我们是否忽略这个字段当它的值为空的时候。
type Model struct { UserIduint `json:"user_id"` Addressstring `json:"address,omitempty"` // if empty, don't encode it at all }
ProductCountuint // required, but zero is a safe default
FavoriteColorstring // a default is ok, but we don't want the empty string
Address *string // not required, but we still want to validate it if it's there
type Request struct { UserId *uint // this is a Required Non-Default Field, so it needs a pointer } type Model struct { UserIduint // but it's not optional, so we don't want the pointer in the model }
UserId *uint `validate:"nonzero"` AgeUint `validate:"min=18"`
Features: Default Fields , Easy Validation
// model type User struct { Namestring `json:"name" validate:"nonzero"` Ageuint `json:"age" validate:"min=1"` Addressstring `json:"address" validate:"nonzero"` } // unmarshalling var userUser if err := json.NewDecoder(jsonByteSlice).Decode(&user); err != nil {...} // marshalling if jsonByteSlice, err := json.Marshal(object); err != nil {...} // validation if errs := validator.Validate(user); errs != nil {...}
Features: Default Fields , Optional Fields , Required Non Default Fields
// model type User struct { Name *string `json:"name"` // required, but no defaults Age *uint `json:"age,omitempty"` // optional Address *string `json:"address,omitempty"` // optional FavoriteColorstring `json:"favoriteColor"` // required, uses defaults } // unmarshalling var userUser if err := json.NewDecoder(jsonByteSlice).Decode(&user); err != nil {...} // marshalling if jsonByteSlice, err := json.Marshal(object); err != nil {...} // validation funcValidate(userUser) { // default - validate value // optional - if non nil, validate value // required non default - validate not nil, then validate value }
Features: Default Fields , Optional Fields , Required Non Default Fields , Easy Validation
UserId *uint `validate:"nonzero,min=100"`
// model type User struct { Name *string `json:"name" validate:"nonzero,min=1"` // required, but no defaults Age *uint `json:"age,omitempty" validate:"min=1"` // optional Address *string `json:"address,omitempty" validate:"min=1"` // optional FavoriteColorstring `json:"favoriteColor"` // required, uses defaults } // unmarshalling var userUser if err := json.NewDecoder(jsonByteSlice).Decode(&user); err != nil {..} // marshalling if jsonByteSlice, err := json.Marshal(object); err != nil {...} // validation if errs := validator.Validate(user); errs != nil {...}
Features: Default Fields , Required Non Default Fields , Easy Validation , Custom Default Fields , Dereferenced Required Fields
// post request type UserPostRequest struct { Name *string `json:"name" validate:"nonzero"` // required, but no defaults Age *uint `json:"age"` // optional Address *string `json:"address"` // optional FavoriteInstrumentstring `json:"favoriteInstrument"` // required, uses defaults FavoriteColor *string `json:"favoriteColor"` // required, uses custom defaults } // model type User struct { Name string `json:"name" validate:"nonzero"` // required Age *uint `json:"age,omitempty" validate:"min=1"` // optional Address *string `json:"address,omitempty" validate:"min=1"` // optional FavoriteInstrumentstring `json:"favoriteInstrument"` // required FavoriteColor string `json:"favoriteColor" validate:"nonzero"` // required } // unmarshalling var postRequestUserPostRequest if err := json.NewDecoder(jsonByteSlice).Decode(&postRequest); err != nil {..} if errs := validator.Validate(postRequest); errs != nil {...} user.Name = *postRequest.Name user.Age = postRequest.Age user.Address = postRequest.Address user.FavoriteInstrument = postRequest.FavoriteInstrument user.FavoriteColor = "blue" if postRequest.FavoriteColor != nil { user.FavoriteColor = *postRequest.FavoriteColor } if errs := validator.Validate(user); errs != nil {...} // marshalling if jsonByteSlice, err := marshal(object); err != nil {...} // validation if errs := validator.Validate(user); errs != nil {...}
参考文献: http://brandonokert.com/2016/04/18/Json-Management-Patterns-In-Go/
Go语言的Json管理模式