优化是否能转换json的判断

This commit is contained in:
2023-08-10 17:14:21 +08:00
parent 7004fb4808
commit 988acc124b
3 changed files with 16 additions and 41 deletions

View File

@ -10,14 +10,10 @@ package json_tool
import (
"encoding/json"
"fmt"
"reflect"
"github.com/tidwall/gjson"
"strings"
"github.com/tidwall/gjson"
"github.com/pkg/errors"
"git.zhangdeman.cn/zhangdeman/util"
)
// MapDataRule 数据映射结果
@ -109,37 +105,7 @@ func (f *Filter) SetValue(result *DynamicJSON, path string, val interface{}, isS
//
// Date : 10:46 下午 2021/3/14
func (f *Filter) isLegalData() bool {
val := reflect.ValueOf(f.data)
switch val.Kind() {
case reflect.Slice:
// slice 情况下,对字节数组进行特殊判断
var (
byteData []byte
ok bool
err error
)
if byteData, ok = f.data.([]byte); ok {
// 字节数组转map或者slice
if err = json.Unmarshal(byteData, &f.data); nil != err {
return false
}
return true
}
return true
case reflect.Map:
return true
case reflect.Struct:
// 结构体转为字符串处理
fallthrough
case reflect.Ptr:
// 指针
var err error
if f.data, err = util.Struct.ToMap(f.data); nil != err {
return false
}
return true
default:
return false
}
byteData, _ := json.Marshal(f.data)
dataRes := gjson.Parse(string(byteData))
return dataRes.IsObject() || dataRes.IsArray()
}