From ad943c5e02254a837fab9d7fe8258033b6ec6cdd Mon Sep 17 00:00:00 2001 From: John Date: Mon, 7 Sep 2020 19:44:11 +0800 Subject: [PATCH] improve gjson.New by using gconv.MapDeep for map/struct --- encoding/gjson/gjson_api_new_load.go | 10 +++++----- encoding/gjson/gjson_z_unit_new_test.go | 22 ++++++++++++++++++++++ encoding/gparser/gparser_api_new_load.go | 2 +- util/gconv/gconv_map.go | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index 3b57b4208..ff402ba61 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -55,8 +55,10 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json { } } default: - rv := reflect.ValueOf(data) - kind := rv.Kind() + var ( + rv = reflect.ValueOf(data) + kind = rv.Kind() + ) if kind == reflect.Ptr { rv = rv.Elem() kind = rv.Kind() @@ -72,9 +74,7 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json { } case reflect.Map, reflect.Struct: i := interface{}(nil) - // Note that it uses Map function implementing the converting. - // Note that it here should not use MapDeep function if you really know what it means. - i = gconv.Map(data, tags) + i = gconv.MapDeep(data, tags) j = &Json{ p: &i, c: byte(gDEFAULT_SPLIT_CHAR), diff --git a/encoding/gjson/gjson_z_unit_new_test.go b/encoding/gjson/gjson_z_unit_new_test.go index a431da8e5..5de7e008c 100644 --- a/encoding/gjson/gjson_z_unit_new_test.go +++ b/encoding/gjson/gjson_z_unit_new_test.go @@ -47,3 +47,25 @@ func Test_Load_NewWithTag(t *testing.T) { t.Assert(j.Get("addr-json"), nil) }) } + +func Test_Load_New_CustomStruct(t *testing.T) { + type Base struct { + Id int + } + type User struct { + Base + Name string + } + user := new(User) + user.Id = 1 + user.Name = "john" + + gtest.C(t, func(t *gtest.T) { + j := gjson.New(user) + t.AssertNE(j, nil) + + s, err := j.ToJsonString() + t.Assert(err, nil) + t.Assert(s == `{"Id":1,"Name":"john"}` || s == `{"Name":"john","Id":1}`, true) + }) +} diff --git a/encoding/gparser/gparser_api_new_load.go b/encoding/gparser/gparser_api_new_load.go index 7c879fdae..d731d1140 100644 --- a/encoding/gparser/gparser_api_new_load.go +++ b/encoding/gparser/gparser_api_new_load.go @@ -10,7 +10,7 @@ import ( "github.com/gogf/gf/encoding/gjson" ) -// New creates a Parser object with any variable type of , but should be a map or +// New creates a Parser object with any variable type of , but should be a map, struct or // slice for data access reason, or it will make no sense. // // The parameter specifies whether using this Json object in concurrent-safe context, which diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 3f36d90c2..54539faaf 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -37,7 +37,7 @@ func MapDeep(value interface{}, tags ...string) map[string]interface{} { // doMapConvert implements the map converting. // It automatically checks and converts json string to map if is string/[]byte. // -// TODO completely implement the recursive converting for all types. +// TODO completely implement the recursive converting for all types, especially the map. func doMapConvert(value interface{}, recursive bool, tags ...string) map[string]interface{} { if value == nil { return nil