前言
近期會(huì)更新一系列開源項(xiàng)目的文章,新的一年會(huì)和大家做更多的開源項(xiàng)目,也歡迎大家加入進(jìn)來。
xutil
今天分享的文章源自于開源項(xiàng)目jinzaigo/xutil
的封裝。
在封裝過程中,勁仔將實(shí)現(xiàn)原理以及相關(guān)實(shí)踐思考,寫成文章分享出來,從而匯總系列文章集合。
PHP轉(zhuǎn)Go
我和勁仔都是PHP轉(zhuǎn)Go,身邊越來越多做PHP的朋友也逐漸在用Go進(jìn)行重構(gòu),重構(gòu)過程中,會(huì)發(fā)現(xiàn)php的json解析操作(系列化與反序列化)是真的香,弱類型語言的各種隱式類型轉(zhuǎn)換,很大程度的減低了程序的復(fù)雜度。
反觀go使用標(biāo)準(zhǔn)庫encoding/json,來做json解析就沒有那么愉快了(只要數(shù)據(jù)類型定義不對,就很容易拋error)
JSON解析實(shí)踐
案例:用go重構(gòu)的服務(wù),對接的上游還是php服務(wù),這時(shí)php接口輸出的json串為{"name":"AppleWatchS8","price":"3199"}
。
其中price字段應(yīng)該得為float類型,但由于php弱類型語言,沒有強(qiáng)制約束輸出類型的機(jī)制,就很容易出現(xiàn)這種輸出類型不對的情況,然后到go服務(wù)里得怎么處理呢?
標(biāo)準(zhǔn)庫encoding/json
package main
import (
"encoding/json"
"fmt"
)
type ProductInfo struct {
Name string `json:"name"`
Price float32 `json:"price"`
}
func main() {
str := "{"name":"AppleWatchS8","price":"3199"}"
data := ProductInfo{}
if err := json.Unmarshal([]byte(str), &data); err != nil {
fmt.Println("error: " + err.Error())
} else {
fmt.Println(data)
}
}
//輸出結(jié)果
//error: json: cannot unmarshal string into Go struct field ProductInfo.price of type float32
顯然,使用go標(biāo)準(zhǔn)庫做json解析,是應(yīng)對不了這種類型不一致的情況的。下面則借助第三方庫的能力來做處理
第三方庫json-iterator
簡單介紹:
執(zhí)行速度:jsoniter 的 Golang 版本可以比標(biāo)準(zhǔn)庫(encoding/json)快 6 倍之多
兩個(gè)特點(diǎn):
-
完全兼容標(biāo)準(zhǔn)庫,也就是API用法完全一樣,原有邏輯代碼不需要改動(dòng),只需要替換import包名
-
提供了一個(gè)PHP兼容模式,可以自動(dòng)轉(zhuǎn)換字符串/數(shù)字弱類型問題,可以將空數(shù)組[]轉(zhuǎn)換為空結(jié)構(gòu)體(解決PHP中的array輸出為[]的問題)。注意,該兼容模式需要手動(dòng)開啟
安裝方式:
go get -u github.com/json-iterator/go
具體代碼實(shí)現(xiàn):
package main
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
func init() {
extra.RegisterFuzzyDecoders() //開啟PHP兼容模式
}
type ProductInfo struct {
Name string `json:"name"`
Price float32 `json:"price"`
}
func main() {
str := "{"name":"AppleWatchS8","price":"3199"}"
data := ProductInfo{}
if err := json.Unmarshal([]byte(str), &data); err != nil {
fmt.Println("error: " + err.Error())
} else {
fmt.Println(data)
}
}
//輸出結(jié)果
//{AppleWatchS8 3199}
看輸出結(jié)果,會(huì)發(fā)現(xiàn)用了這個(gè)庫并且開啟了PHP兼容模式,json中price字段string類型,就會(huì)自動(dòng)轉(zhuǎn)換為結(jié)構(gòu)體中定義的float32類型。
這樣我們在使用price字段處理業(yè)務(wù)邏輯時(shí),就只需要按float32做處理就行,不用進(jìn)行類型斷言。這個(gè)庫解決了json解析類型轉(zhuǎn)換問題的同時(shí),也能極大的提高我們開發(fā)效率。
收集到開源工具包xutil中
這個(gè)第三庫用起來如此方便,那肯定是要收錄進(jìn)來的,將替換包名、手動(dòng)開啟PHP兼容模式、還有常用的API方法(系列化與反序列化操作),統(tǒng)一封裝進(jìn)來,簡化使用流程。
同時(shí),為了便于后續(xù)擴(kuò)展更多的兼容模式,所以將代碼都放在xjson目錄下
以上這個(gè)思路也適用于大家封裝自己內(nèi)部使用的工具庫。
使用示例:
go get -u github.com/jinzaigo/xutil
之后,
import github.com/jinzaigo/xutil/xjson
,
即可通過xjson.Unmarshal()等方法,進(jìn)行json解析操作。
示例代碼:
package main
import (
"fmt"
"github.com/jinzaigo/xutil/xjson"
)
type ProductInfo struct {
Name string `json:"name"`
Price float32 `json:"price"`
}
func main() {
str := "{"name":"AppleWatchS8","price":"3199"}"
data := ProductInfo{}
if err := xjson.Unmarshal([]byte(str), &data); err != nil {
fmt.Println("error: " + err.Error())
} else {
fmt.Println(data)
}
}
總結(jié)
業(yè)務(wù)系統(tǒng)從php轉(zhuǎn)go,或go對接php服務(wù),都會(huì)遇到這個(gè)因?yàn)閿?shù)據(jù)類型不一致導(dǎo)致json解析錯(cuò)誤的共性問題。
使用第三方庫json-iterator能很好的解決我們的痛點(diǎn),并且比標(biāo)準(zhǔn)庫執(zhí)行速度還更快。
收錄到開源項(xiàng)目中,更好的幫助到需要的朋友。文章來源:http://www.zghlxwxcb.cn/news/detail-779381.html
一起學(xué)習(xí)
歡迎在CSDN私信我,也歡迎 加我好友 一起學(xué)習(xí)。文章來源地址http://www.zghlxwxcb.cn/news/detail-779381.html
到了這里,關(guān)于PHP轉(zhuǎn)Go實(shí)踐:xjson解析神器「開源工具集」的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!