优化是否能转换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

7
go.mod
View File

@ -4,16 +4,17 @@ go 1.20
require (
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230806071338-020ba99fe8fe
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810085948-024a0e5e963a
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810085842-1e901000e998
github.com/Jeffail/gabs v1.4.0
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.8.1
github.com/tidwall/gjson v1.15.0
github.com/tidwall/gjson v1.16.0
github.com/tidwall/sjson v1.2.5
)
require (
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect

8
go.sum
View File

@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20220627070212-c590a0a1c216 h1:5M6Y
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20220627070212-c590a0a1c216/go.mod h1:8Jh+q/L0pWAKQy67m8AKIs0WGGrVKypoSZZbBm4nDVc=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 h1:+Lg4vXFEiWVKjhUJdXuoP0AgjGT49oqJ3301STnZErk=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10/go.mod h1:+Lc0zYF8sylRi75A7NGmObrLxugwAZa8WVpWh2eh5X0=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220626081130-cbac0b676fb8/go.mod h1:G2/OKMbEn89d+YUXQtv9Nlh0LGg14pOqDnbOgBTTRXY=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151 h1:j537bRLQL1FlkdXTIaT9Ecjx5eogkPsGiTOWIEFQlc8=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151/go.mod h1:SyRTkOz6gxUVn3S/Qtkf+rhKV0I1ym8lwsT8YjggYFs=
@ -15,6 +17,8 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20230625100543-a0e8f3b7c22e h1:TPVXQjua
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230625100543-a0e8f3b7c22e/go.mod h1:6+7/YFzLeIclsqMu9HPBosQpPPLnxtrge/ftjU3qj8k=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305 h1:6Bs/cQP+eKABHB/01uHQI15PwKbo7n8HNx7nIFUGBp0=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810085948-024a0e5e963a h1:PED+zfKJdSbewYQNpcjj3uXoGDSA5+8LBGScObfuHAA=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810085948-024a0e5e963a/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505064614-5e785171ed67 h1:DH9K3fNddpFxRGLkcLP5MHsAQVinpWpmGzbVBf8yrKM=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505064614-5e785171ed67/go.mod h1:2jc48WuVoHxZjkvlBewzp+ey8khP1K4OOcibVD1yL2k=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505070245-b182b21e039b h1:ZwAA10/+v3FFAq5/EzjXdXDodmKppXb5gBkCcgaYVBo=
@ -25,6 +29,8 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230626100818-1ca4b993fb24 h1:aSH9W
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230626100818-1ca4b993fb24/go.mod h1:Zl2pqhTvNpr5SrKPfZ5JDt+FkyimxnYxLfpvTgaLG+c=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230806071338-020ba99fe8fe h1:qNGgB2Yl9iV8Y7wLsYE3xf8pF1ZOX7VjH4BwOzM6iEE=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230806071338-020ba99fe8fe/go.mod h1:Zl2pqhTvNpr5SrKPfZ5JDt+FkyimxnYxLfpvTgaLG+c=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810085842-1e901000e998 h1:+qefkhBULVZbTGxJO4JqT1C1JQLaccJA6GLKP1qImqE=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810085842-1e901000e998/go.mod h1:WcNcIIxTSpvjpEhfE64Ktis3q+Fw+Onw7NC2/I1U2Qs=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
@ -73,6 +79,8 @@ github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.15.0 h1:5n/pM+v3r5ujuNl4YLZLsQ+UE5jlkLVm7jMzT5Mpolw=
github.com/tidwall/gjson v1.15.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=

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()
}