Skip to content

DaoWind/json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json

    The json package was based on go1.13.8 encoding/json package
    New feature: 
            "must" tag
    Description: 
            if json bytes missing field or the field with empty value,  Unmarshal will failed.

Sample Code:



import (
        "github.com/DaoWind/json"    
        "fmt"
)

type Msg struct {
        Bank       string `json:"bank,omitempty,must"`
        Code       string `json:"code,omitempty,must"`
        Version    string `json:"version,omitempty,must"`
        MsgId      string `json:"msgId,omitempty,must"`
}

// 1: Good example without error
var body = "{\"msgId\":\"202002280001\",\"bank\":\"102100009980\",\"version\":\"1.0.0\",\"code\":\"AsstRegr\"}"
// 2: Fail example with error(Field(code) Must Be Not-Empty!)
var body = "{\"msgId\":\"202002280001\",\"bank\":\"102100009980\",\"version\":\"1.0.0\",\"code\":\"\"}"
// 3: Fail example with error(Field(code) Must Be Exist!)
var body = "{\"msgId\":\"202002280001\",\"bank\":\"102100009980\",\"version\":\"1.0.0\"}"


func main() {
        var msg Msg
        err := json.Unmarshal([]byte(body), &msg)
        if err != nil {
                fmt.Printf("Got Error:%s\n", err.Error())
        } else {
                fmt.Printf("Success!\n")
        }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages