新聞中心
?xutil
今天分享的文章源自于開源項目jinzaigo/xutil的封裝。

在封裝過程中,勁仔將實現(xiàn)原理以及相關(guān)實踐思考,寫成文章分享出來,從而匯總系列文章集合。
PHP轉(zhuǎn)Go
我和勁仔都是PHP轉(zhuǎn)Go,身邊越來越多做PHP的朋友也逐漸在用Go進行重構(gòu),重構(gòu)過程中,會發(fā)現(xiàn)php的json解析操作(系列化與反序列化)是真的香,弱類型語言的各種隱式類型轉(zhuǎn)換,很大程度的減低了程序的復(fù)雜度。
反觀go使用標準庫encoding/json,來做json解析就沒有那么愉快了(只要數(shù)據(jù)類型定義不對,就很容易拋error)
JSON解析實踐
案例:用go重構(gòu)的服務(wù),對接的上游還是php服務(wù),這時php接口輸出的json串為{"name":"AppleWatchS8","price":"3199"} ,其中price字段應(yīng)該得為float類。
但由于php弱類型語言,沒有強制約束輸出類型的機制,就很容易出現(xiàn)這種輸出類型不對的情況,然后到go服務(wù)里得怎么處理呢?
標準庫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標準庫做json解析,是應(yīng)對不了這種類型不一致的情況的。下面則借助第三方庫的能力來做處理
第三方庫json-iterator
簡單介紹:
執(zhí)行速度:jsoniter 的 Golang 版本可以比標準庫(encoding/json)快 6 倍之多
兩個特點:
- 完全兼容標準庫,也就是API用法完全一樣,原有邏輯代碼不需要改動,只需要替換import包名
- 提供了一個PHP兼容模式,可以自動轉(zhuǎn)換字符串/數(shù)字弱類型問題,可以將空數(shù)組[]轉(zhuǎn)換為空結(jié)構(gòu)體(解決PHP中的array輸出為[]的問題)。注意,該兼容模式需要手動開啟
安裝方式:
go get -u github.com/json-iterator/go
具體代碼實現(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é)果,會發(fā)現(xiàn)用了這個庫并且開啟了PHP兼容模式,json中price字段string類型,就會自動轉(zhuǎn)換為結(jié)構(gòu)體中定義的float32類型。
這樣我們在使用price字段處理業(yè)務(wù)邏輯時,就只需要按float32做處理就行,不用進行類型斷言。
這個庫解決了json解析類型轉(zhuǎn)換問題的同時,也能極大的提高我們開發(fā)效率。
收集到開源工具包xutil中
這個第三庫用起來如此方便,那肯定是要收錄進來的,將替換包名、手動開啟PHP兼容模式、還有常用的API方法(系列化與反序列化操作),統(tǒng)一封裝進來,簡化使用流程。
同時,為了便于后續(xù)擴展更多的兼容模式,所以將代碼都放在xjson目錄下
以上這個思路也適用于大家封裝自己內(nèi)部使用的工具庫。
使用示例:
go get -u github.com/jinzaigo/xutil?之后,import github.com/jinzaigo/xutil/xjson,即可通過xjson.Unmarshal()等方法,進行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ù),都會遇到這個因為數(shù)據(jù)類型不一致導(dǎo)致json解析錯誤的共性問題。
使用第三方庫json-iterator能很好的解決我們的痛點,并且比標準庫執(zhí)行速度還更快。
本文轉(zhuǎn)載自微信公眾號「 程序員升級打怪之旅」,作者「王中陽Go」,可以通過以下二維碼關(guān)注。
轉(zhuǎn)載本文請聯(lián)系「 程序員升級打怪之旅」公眾號。
本文名稱:PHP轉(zhuǎn)Go實踐:xjson解析神器「開源工具集」
本文路徑:http://www.5511xx.com/article/codcsoo.html


咨詢
建站咨詢
