gjson_read优化类型判断

This commit is contained in:
白茶清欢 2025-05-24 18:56:28 +08:00
parent dfae2fda76
commit 2e1ece4c82

View File

@ -14,6 +14,7 @@ import (
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack" "git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
"git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"strings"
) )
// NewGjsonRead ... // NewGjsonRead ...
@ -53,7 +54,14 @@ func (g *gjsonRead) Type(dataPath string) string {
case gjson.String: case gjson.String:
return consts.DataTypeString.String() return consts.DataTypeString.String()
case gjson.Number: case gjson.Number:
return consts.DataTypeFloat64.String() valStr := pathRes.String()
if strings.Contains(valStr, ".") {
return consts.DataTypeFloat64.String()
}
if strings.HasPrefix(valStr, "-") {
return consts.DataTypeInt64.String()
}
return consts.DataTypeUint64.String()
case gjson.Null: case gjson.Null:
return consts.DataTypeAny.String() return consts.DataTypeAny.String()
case gjson.True, gjson.False: case gjson.True, gjson.False:
@ -63,15 +71,15 @@ func (g *gjsonRead) Type(dataPath string) string {
} }
func (g *gjsonRead) Int(dataPath string) (int64, error) { func (g *gjsonRead) Int(dataPath string) (int64, error) {
return gjson_hack.Int(g.gjsonResult.Get(dataPath)) return gjson_hack.Int[int64](g.gjsonResult.Get(dataPath))
} }
func (g *gjsonRead) Uint(dataPath string) (uint64, error) { func (g *gjsonRead) Uint(dataPath string) (uint64, error) {
return gjson_hack.Uint(g.gjsonResult.Get(dataPath)) return gjson_hack.Uint[uint64](g.gjsonResult.Get(dataPath))
} }
func (g *gjsonRead) Float(dataPath string) (float64, error) { func (g *gjsonRead) Float(dataPath string) (float64, error) {
return gjson_hack.Float64(g.gjsonResult.Get(dataPath)) return gjson_hack.Float[float64](g.gjsonResult.Get(dataPath))
} }
func (g *gjsonRead) Bool(dataPath string) (bool, error) { func (g *gjsonRead) Bool(dataPath string) (bool, error) {