Json 做爲一種重要的數據格式,具備良好的可讀性以及自描述性,普遍地應用在各類數據傳輸場景中。Go 語言裏面原生支持了這種數據格式的序列化以及反序列化,內部使用反射機制實現,性能有點差,在高度依賴 json 解析的應用裏,每每會成爲性能瓶頸,好在已有不少第三方庫幫咱們解決了這個問題,可是這麼多庫,對於像我這種有選擇困難症的人來講,到底要怎麼選擇呢,下面就給你們來一一分析一下node
go get -u github.com/pquerna/ffjson
原生的庫性能比較差的主要緣由是使用了不少反射的機制,爲了解決這個問題,ffjson 經過預編譯生成代碼,類型的判斷在預編譯階段已經肯定,避免了在運行時的反射git
但也所以在編譯前須要多一個步驟,須要先生成 ffjson 代碼,生成代碼只須要執行 ffjson <file.go>
就能夠了,其中 file.go
是一個包含 json 結構體定義的 go 文件。注意這裏 ffjson 是這個庫提供的一個代碼生成工具,直接執行上面的 go get
會把這個工具安裝在 $GOPATH/bin
目錄下,把 $GOPATH/bin
加到 $PATH
環境變量裏面,能夠全局訪問github
另外,若是有些結構,不想讓 ffjson 生成代碼,能夠經過增長註釋的方式golang
// ffjson: skip type Foo struct { Bar string } // ffjson: nodecoder type Foo struct { Bar string }
go get -u github.com/mailru/easyjson/...
easyjson 的思想和 ffjson 是一致的,都是增長一個預編譯的過程,預先生成對應結構的序列化反序列化代碼,除此以外,easyjson 還放棄了一些原生庫裏面支持的一些沒必要要的特性,好比:key 類型聲明,key 大小寫不敏感等等,以達到更高的性能json
生成代碼執行 easyjson -all <file.go>
便可,若是不指定 -all
參數,只會對帶有 //easyjson:json
的結構生成代碼工具
//easyjson:json type A struct { Bar string }
go get -u github.com/json-iterator/go
這是一個很神奇的庫,滴滴開發的,不像 easyjson 和 ffjson 都使用了預編譯,並且 100% 兼容原生庫,可是性能超級好,也不知道怎麼實現的,若是有人知道的話,能夠告訴我一下嗎?性能
使用上面,你只要把全部的測試
import "encoding/json"
替換成jsonp
import "github.com/json-iterator/go" var json = jsoniter.ConfigCompatibleWithStandardLibrary
就能夠了,其它都不須要動spa
go get -u github.com/ugorji/go/codec
這個庫裏面其實包含不少內容,json 只是其中的一個功能,比較老,使用起來比較麻煩,性能也不是很好
go get -u github.com/buger/jsonparser
嚴格來講,這個庫不屬於 json 序列化的庫,只是提供了一些 json 解析的接口,使用的時候須要本身去設置結構裏面的值,事實上,每次調用都須要從新解析 json 對象,性能並非很好
就像名字暗示的那樣,這個庫只是一個解析庫,並無序列化的接口
對上面這些 json 庫,做了一些性能測試,測試代碼在:https://github.com/hatlonely/...,下面是在個人 Macbook 上測試的結果(實際結果和庫的版本以及機器環境有關,建議本身再測試一遍):
BenchmarkMarshalStdJson-4 1000000 1097 ns/op BenchmarkMarshalJsonIterator-4 2000000 781 ns/op BenchmarkMarshalFfjson-4 2000000 941 ns/op BenchmarkMarshalEasyjson-4 3000000 513 ns/op BenchmarkMarshalCodecJson-4 1000000 1074 ns/op BenchmarkMarshalCodecJsonWithBufio-4 1000000 2161 ns/op BenchmarkUnMarshalStdJson-4 500000 2512 ns/op BenchmarkUnMarshalJsonIterator-4 2000000 591 ns/op BenchmarkUnMarshalFfjson-4 1000000 1127 ns/op BenchmarkUnMarshalEasyjson-4 2000000 608 ns/op BenchmarkUnMarshalCodecJson-4 20000 122694 ns/op BenchmarkUnMarshalCodecJsonWithBufio-4 500000 3417 ns/op BenchmarkUnMarshalJsonparser-4 2000000 877 ns/op
從上面的結果能夠看出來:
因此綜合考慮,建議你們使用 jsoniter,若是追求極致的性能,考慮 easyjson
ffjson: https://github.com/pquerna/ff...
easyjson: https://github.com/mailru/eas...
jsoniter: https://github.com/json-itera...
jsonparser: https://github.com/buger/json...
codecjson: http://ugorji.net/blog/go-cod...
轉載請註明出處
本文連接: http://hatlonely.github.io/20...