1 Star 0 Fork 0

xlizy / common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
json.go 705 Bytes
一键复制 编辑 原始数据 按行查看 历史
xlizy 提交于 2024-04-09 15:35 . fix
package json
import (
eJson "encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
"github.com/kataras/iris/v12/context"
)
var js = jsoniter.ConfigCompatibleWithStandardLibrary
func init() {
extra.RegisterFuzzyDecoders() //开启PHP兼容模式
}
func ToJsonStr(obj any) string {
if obj == nil {
return "{}"
}
res, err := eJson.Marshal(&obj)
if err != nil {
return "{}"
} else {
return string(res)
}
}
func ToObj(jsonStr string, obj any) {
eJson.Unmarshal([]byte(jsonStr), &obj)
}
func ReadCtxJson(ctx *context.Context, obj any) {
jsonStr := ""
ctx.ReadBody(&jsonStr)
b := []byte(jsonStr)
if jsoniter.Valid(b) {
js.Unmarshal(b, &obj)
}
}
Go
1
https://gitee.com/xlizy/common-go.git
git@gitee.com:xlizy/common-go.git
xlizy
common-go
common-go
v0.2.2

搜索帮助