bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Golang高性能json包:easyjson-創新互聯

簡介

easyjson是什么呢? 根據官網介紹,easyjson是提供高效快速且易用的結構體structs<-->json轉換包。easyjson并沒有使用反射方式實現,所以性能比其他的json包該4-5倍,比golang 自帶的json包快2-3倍。 easyjson目標是維持生成去代碼簡單,以致于它可以輕松地進行優化或固定。

創新互聯長期為成百上千客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為鉛山企業提供專業的網站設計、網站建設,鉛山網站改版等技術服務。擁有10余年豐富建站經驗和眾多成功案例,為您定制開發。

安裝

go get -u github.com/mailru/easyjson/go install  github.com/mailru/easyjson/easyjsonorgo build -o easyjson github.com/mailru/easyjson/easyjson

驗證是否安裝成功。

$ easyjson
Usage of D:\Code\go\bin\easyjson.exe:
  -all        generate marshaler/unmarshalers for all structs in a file
  -build_tags string        build tags to add to generated file
  -leave_temps        do not delete temporary files
  -lower_camel_case        use lowerCamelCase names instead of CamelCase by default
  -no_std_marshalers        don't generate MarshalJSON/UnmarshalJSON funcs
  -noformat        do not run 'gofmt -w' on output file
  -omit_empty        omit empty fields by default

   string
        specify the filename of the output
  -pkg        process the whole package instead of just the given file
  -snake_case        use snake_case names instead of CamelCase by default
  -stubs        only generate stubs for marshaler/unmarshaler funcs

其中有幾個選項需要注意:

-lower_camel_case:將結構體字段field首字母改為小寫。如Name=>name。  
-build_tags string:將指定的string生成到生成的go文件頭部。  
-no_std_marshalers:不為結構體生成MarshalJSON/UnmarshalJSON函數。  
-omit_empty:沒有賦值的field可以不生成到json,否則field為該字段類型的默認值。-output_filename:定義生成的文件名稱。-pkg:對包內指定有`//easyjson:json`結構體生成對應的easyjson配置。-snke_case:可以下劃線的field如`Name_Student`改為`name_student`。

使用

記得在需要使用easyjson的結構體上加上//easyjson:json。 如下:

//easyjson:jsontype School struct {    Name string        `json:"name"`
    Addr string        `json:"addr"`}//easyjson:jsontype Student struct {
    Id       int       `json:"id"`
    Name     string    `json:"s_name"`
    School   School    `json:"s_chool"`
    Birthday time.Time `json:"birthday"`}

在結構體包下執行

easyjson  -all student.go

此時在該目錄下出現一個新的文件。

// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.package easyjsonimport (
    json "encoding/json"
    easyjson "github.com/mailru/easyjson"
    jlexer "github.com/mailru/easyjson/jlexer"
    jwriter "github.com/mailru/easyjson/jwriter")// suppress unused package warningvar (
    _ *json.RawMessage
    _ *jlexer.Lexer
    _ *jwriter.Writer
    _ easyjson.Marshaler)func easyjsonB83d7b77DecodeStudygoEasyjson(in *jlexer.Lexer, out *Student) {
    isTopLevel := in.IsStart()    if in.IsNull() {        if isTopLevel {            in.Consumed()
        }        in.Skip()        return
    }    in.Delim('{')    for !in.IsDelim('}') {
        key := in.UnsafeString()        in.WantColon()        if in.IsNull() {            in.Skip()            in.WantComma()            continue
        }        switch key {        case "id":            out.Id = int(in.Int())        case "s_name":            out.Name = string(in.String())        case "s_chool":
            easyjsonB83d7b77DecodeStudygoEasyjson1(in, &out.School)        case "birthday":            if data := in.Raw(); in.Ok() {                in.AddError((out.Birthday).UnmarshalJSON(data))
            }        default:            in.SkipRecursive()
        }        in.WantComma()
    }    in.Delim('}')    if isTopLevel {        in.Consumed()
    }
}func easyjsonB83d7b77EncodeStudygoEasyjson(out *jwriter.Writer, in Student) {    out.RawByte('{')
    first := true
    _ = first    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"id\":")    out.Int(int(in.Id))    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"s_name\":")    out.String(string(in.Name))    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"s_chool\":")
    easyjsonB83d7b77EncodeStudygoEasyjson1(out, in.School)    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"birthday\":")    out.Raw((in.Birthday).MarshalJSON())    out.RawByte('}')
}// MarshalJSON supports json.Marshaler interfacefunc (v Student) MarshalJSON() ([]byte, error) {
    w := jwriter.Writer{}
    easyjsonB83d7b77EncodeStudygoEasyjson(&w, v)    return w.Buffer.BuildBytes(), w.Error
}// MarshalEasyJSON supports easyjson.Marshaler interfacefunc (v Student) MarshalEasyJSON(w *jwriter.Writer) {
    easyjsonB83d7b77EncodeStudygoEasyjson(w, v)
}// UnmarshalJSON supports json.Unmarshaler interfacefunc (v *Student) UnmarshalJSON(data []byte) error {
    r := jlexer.Lexer{Data: data}
    easyjsonB83d7b77DecodeStudygoEasyjson(&r, v)    return r.Error()
}// UnmarshalEasyJSON supports easyjson.Unmarshaler interfacefunc (v *Student) UnmarshalEasyJSON(l *jlexer.Lexer) {
    easyjsonB83d7b77DecodeStudygoEasyjson(l, v)
}func easyjsonB83d7b77DecodeStudygoEasyjson1(in *jlexer.Lexer, out *School) {
    isTopLevel := in.IsStart()    if in.IsNull() {        if isTopLevel {            in.Consumed()
        }        in.Skip()        return
    }    in.Delim('{')    for !in.IsDelim('}') {
        key := in.UnsafeString()        in.WantColon()        if in.IsNull() {            in.Skip()            in.WantComma()            continue
        }        switch key {        case "name":            out.Name = string(in.String())        case "addr":            out.Addr = string(in.String())        default:            in.SkipRecursive()
        }        in.WantComma()
    }    in.Delim('}')    if isTopLevel {        in.Consumed()
    }
}func easyjsonB83d7b77EncodeStudygoEasyjson1(out *jwriter.Writer, in School) {    out.RawByte('{')
    first := true
    _ = first    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"name\":")    out.String(string(in.Name))    if !first {        out.RawByte(',')
    }
    first = false
    out.RawString("\"addr\":")    out.String(string(in.Addr))    out.RawByte('}')
}

現在可以寫一個測試類啦。

package mainimport (    "studygo/easyjson"
    "time"
    "fmt")func main(){
    s:=easyjson.Student{
        Id: 11,
        Name:"qq",
        School:easyjson.School{
            Name:"CUMT",
            Addr:"xz",
        },
        Birthday:time.Now(),
    }
    bt,err:=s.MarshalJSON()
    fmt.Println(string(bt),err)
    json:=`{"id":11,"s_name":"qq","s_chool":{"name":"CUMT","addr":"xz"},"birthday":"2017-08-04T20:58:07.9894603+08:00"}`
    ss:=easyjson.Student{}
    ss.UnmarshalJSON([]byte(json))
    fmt.Println(ss)
}

運行結果:

{"id":11,"s_name":"qq","s_chool":{"name":"CUMT","addr":"xz"},"birthday":"2017-08-04T20:58:07.9894603+08:00"} <nil>
{121  {CwwwwwwwUMT xzwwwww} 2017-08-04 20:52:03.4066002 +0800 CST}

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章題目:Golang高性能json包:easyjson-創新互聯
轉載注明:http://vcdvsql.cn/article26/cedecg.html

成都網站建設公司_創新互聯,為您提供商城網站營銷型網站建設網站導航網站建設手機網站建設網站策劃

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

搜索引擎優化