From eb11061bd2e0a38ebf7da050c5872843d5cd786c Mon Sep 17 00:00:00 2001 From: the harder the luckier Date: Mon, 4 Sep 2023 21:19:22 +0800 Subject: [PATCH] fix: gjson encode to string expect inconformity(issue 2520) (#2928) --- encoding/gjson/gjson_z_unit_test.go | 13 +++++++++++++ util/gconv/gconv_map.go | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/encoding/gjson/gjson_z_unit_test.go b/encoding/gjson/gjson_z_unit_test.go index 5ab828a8c0e..55faf5d0490 100644 --- a/encoding/gjson/gjson_z_unit_test.go +++ b/encoding/gjson/gjson_z_unit_test.go @@ -14,6 +14,7 @@ import ( "github.com/gogf/gf/v2/container/gvar" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/util/gconv" ) @@ -587,3 +588,15 @@ func Test_Issue1747(t *testing.T) { t.Assert(j.Get("2"), `336371793314971759`) }) } + +// https://github.com/gogf/gf/issues/2520 +func Test_Issue2520(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type test struct { + Unique *gvar.Var `json:"unique"` + } + + t2 := test{Unique: gvar.New(gtime.Date())} + t.Assert(gjson.MustEncodeString(t2), gjson.New(t2).MustToJsonString()) + }) +} diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 9e3605cb58f..f9a510eeca3 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -306,7 +306,9 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in dataMap[mapK] = mapV } } - return dataMap + if len(dataMap) > 0 { + return dataMap + } } // Using reflect for converting. var (