增加基于gsjon实现的JsonRead
This commit is contained in:
parent
761058f8fe
commit
8489acbef8
@ -13,10 +13,12 @@ type IJsonRead interface {
|
|||||||
Exist(dataPath string) bool
|
Exist(dataPath string) bool
|
||||||
// IsNil 指定路径是否为nil
|
// IsNil 指定路径是否为nil
|
||||||
IsNil(dataPath string) bool
|
IsNil(dataPath string) bool
|
||||||
|
// Type 路径数据类型
|
||||||
|
Type(dataPath string) string
|
||||||
// Int 转换为int类型
|
// Int 转换为int类型
|
||||||
Int(dataPath string) (int, error)
|
Int(dataPath string) (int64, error)
|
||||||
// Uint 转换为uint类型
|
// Uint 转换为uint类型
|
||||||
Uint(dataPath string) (uint, error)
|
Uint(dataPath string) (uint64, error)
|
||||||
// Float 转换为float类型
|
// Float 转换为float类型
|
||||||
Float(dataPath string) (float64, error)
|
Float(dataPath string) (float64, error)
|
||||||
// String 转换为string类型
|
// String 转换为string类型
|
||||||
|
120
implement/gjson_read.go
Normal file
120
implement/gjson_read.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
// Package implement ...
|
||||||
|
//
|
||||||
|
// Description : implement ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-05-06 11:00
|
||||||
|
package implement
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewGjsonRead ...
|
||||||
|
func NewGjsonRead(sourceData string) abstract.IJsonRead {
|
||||||
|
return &gjsonRead{
|
||||||
|
sourceData: sourceData,
|
||||||
|
gjsonResult: gjson.Parse(sourceData),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type gjsonRead struct {
|
||||||
|
sourceData string
|
||||||
|
gjsonResult gjson.Result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Exist(dataPath string) bool {
|
||||||
|
return g.gjsonResult.Get(dataPath).Exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) IsNil(dataPath string) bool {
|
||||||
|
return g.gjsonResult.Get(dataPath).Value() == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Type(dataPath string) string {
|
||||||
|
pathRes := g.gjsonResult.Get(dataPath)
|
||||||
|
if pathRes.IsObject() { // map
|
||||||
|
return consts.DataTypeMapStrAny.String()
|
||||||
|
}
|
||||||
|
if pathRes.IsArray() { // slice
|
||||||
|
return consts.DataTypeSliceAny.String()
|
||||||
|
}
|
||||||
|
if pathRes.IsBool() { // bool
|
||||||
|
return consts.DataTypeBool.String()
|
||||||
|
}
|
||||||
|
dataType := pathRes.Type
|
||||||
|
switch dataType {
|
||||||
|
case gjson.String:
|
||||||
|
return consts.DataTypeString.String()
|
||||||
|
case gjson.Number:
|
||||||
|
return consts.DataTypeFloat64.String()
|
||||||
|
case gjson.Null:
|
||||||
|
return consts.DataTypeAny.String()
|
||||||
|
case gjson.True, gjson.False:
|
||||||
|
return consts.DataTypeBool.String()
|
||||||
|
}
|
||||||
|
return consts.DataTypeAny.String() // any类型兜底
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Int(dataPath string) (int64, error) {
|
||||||
|
return gjson_hack.Int(g.gjsonResult.Get(dataPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Uint(dataPath string) (uint64, error) {
|
||||||
|
return gjson_hack.Uint(g.gjsonResult.Get(dataPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Float(dataPath string) (float64, error) {
|
||||||
|
return gjson_hack.Float64(g.gjsonResult.Get(dataPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) String(dataPath string) (string, error) {
|
||||||
|
if !g.Exist(dataPath) {
|
||||||
|
return "", errors.New(dataPath + ": not found")
|
||||||
|
}
|
||||||
|
return g.gjsonResult.Get(dataPath).String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Map(dataPath string) (map[string]any, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
dataMap map[string]any
|
||||||
|
)
|
||||||
|
if err = g.MapWithReceiver(dataPath, &dataMap); nil != err {
|
||||||
|
return map[string]any{}, err
|
||||||
|
}
|
||||||
|
return dataMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) MapWithReceiver(dataPath string, receiver any) error {
|
||||||
|
strVal, err := g.String(dataPath)
|
||||||
|
if nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) Array(dataPath string) ([]any, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
dataArr []any
|
||||||
|
)
|
||||||
|
if err = g.ArrayWithReceiver(dataPath, &dataArr); nil != err {
|
||||||
|
return []any{}, err
|
||||||
|
}
|
||||||
|
return dataArr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gjsonRead) ArrayWithReceiver(dataPath string, receiver any) error {
|
||||||
|
strVal, err := g.String(dataPath)
|
||||||
|
if nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user