From d25e579ecf3bd35bd8c51cb8e888da0030607b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 1 Dec 2024 18:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B0=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E4=B9=8B=E5=90=8E=E7=9A=84=E6=95=B0=E7=BB=84=E3=80=81?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=BD=AC=E5=9B=9E?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E3=80=81=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson_hack/path.go | 59 +++++++++++++++++++++++++++++++++++++++++ gjson_hack/path_test.go | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/gjson_hack/path.go b/gjson_hack/path.go index 57bf1ea..2e7995a 100644 --- a/gjson_hack/path.go +++ b/gjson_hack/path.go @@ -183,3 +183,62 @@ func ExpandArrayPath(jsonStr string, pathConfig string, mapConfig string, expend } return nil } + +// IsObject 判断是否是对象, 兼容序列化之后的对象 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:04 2024/12/1 +func IsObject(gjsonResult gjson.Result) bool { + return gjsonResult.IsObject() || gjson.Parse(gjsonResult.String()).IsObject() +} + +// IsArray 判断是否为数组 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:05 2024/12/1 +func IsArray(gjsonResult gjson.Result) bool { + return gjsonResult.IsArray() || gjson.Parse(gjsonResult.String()).IsArray() +} + +// Object 兼容序列化之后的对象 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:09 2024/12/1 +func Object(gjsonResult gjson.Result) gjson.Result { + res := gjson.Parse(gjsonResult.String()) + if res.IsObject() { + return res + } + return gjsonResult +} + +// Array ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:09 2024/12/1 +func Array(gjsonResult gjson.Result) gjson.Result { + res := gjson.Parse(gjsonResult.String()) + if res.IsArray() { + return res + } + return gjsonResult +} + +// Result ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:12 2024/12/1 +func Result(gjsonResult gjson.Result) gjson.Result { + if IsObject(gjsonResult) { + return gjsonResult + } + if IsArray(gjsonResult) { + return Array(gjsonResult) + } + return gjsonResult +} diff --git a/gjson_hack/path_test.go b/gjson_hack/path_test.go index e607098..1848b11 100644 --- a/gjson_hack/path_test.go +++ b/gjson_hack/path_test.go @@ -165,7 +165,7 @@ func TestExpandArrayPath(t *testing.T) { } byteData, _ := json.Marshal(mapData) jsonStr := string(byteData) - // fmt.Println(jsonStr) + var pathExpendRes = &ExpendArrayResult{ PathList: nil, PathMap: nil,