diff --git a/container/glist/glist.go b/container/glist/glist.go index d3e82a082..3dab3ee86 100644 --- a/container/glist/glist.go +++ b/container/glist/glist.go @@ -507,7 +507,7 @@ func (l *List) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (l *List) MarshalJSON() ([]byte, error) { +func (l List) MarshalJSON() ([]byte, error) { return json.Marshal(l.FrontAll()) } diff --git a/container/gmap/gmap_hash_any_any_map.go b/container/gmap/gmap_hash_any_any_map.go index 9b268ae0d..05e451945 100644 --- a/container/gmap/gmap_hash_any_any_map.go +++ b/container/gmap/gmap_hash_any_any_map.go @@ -461,7 +461,7 @@ func (m *AnyAnyMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *AnyAnyMap) MarshalJSON() ([]byte, error) { +func (m AnyAnyMap) MarshalJSON() ([]byte, error) { return json.Marshal(gconv.Map(m.Map())) } diff --git a/container/gmap/gmap_hash_int_any_map.go b/container/gmap/gmap_hash_int_any_map.go index 15a0953e0..a4e509cd4 100644 --- a/container/gmap/gmap_hash_int_any_map.go +++ b/container/gmap/gmap_hash_int_any_map.go @@ -460,7 +460,7 @@ func (m *IntAnyMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *IntAnyMap) MarshalJSON() ([]byte, error) { +func (m IntAnyMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_hash_int_int_map.go b/container/gmap/gmap_hash_int_int_map.go index 36e2f74fb..8fe1fa7f2 100644 --- a/container/gmap/gmap_hash_int_int_map.go +++ b/container/gmap/gmap_hash_int_int_map.go @@ -431,7 +431,7 @@ func (m *IntIntMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *IntIntMap) MarshalJSON() ([]byte, error) { +func (m IntIntMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_hash_int_str_map.go b/container/gmap/gmap_hash_int_str_map.go index 94a026ef8..27697bc44 100644 --- a/container/gmap/gmap_hash_int_str_map.go +++ b/container/gmap/gmap_hash_int_str_map.go @@ -431,7 +431,7 @@ func (m *IntStrMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *IntStrMap) MarshalJSON() ([]byte, error) { +func (m IntStrMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_hash_str_any_map.go b/container/gmap/gmap_hash_str_any_map.go index 8846a2971..27708b230 100644 --- a/container/gmap/gmap_hash_str_any_map.go +++ b/container/gmap/gmap_hash_str_any_map.go @@ -456,7 +456,7 @@ func (m *StrAnyMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *StrAnyMap) MarshalJSON() ([]byte, error) { +func (m StrAnyMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_hash_str_int_map.go b/container/gmap/gmap_hash_str_int_map.go index a6c7fc773..2041473b7 100644 --- a/container/gmap/gmap_hash_str_int_map.go +++ b/container/gmap/gmap_hash_str_int_map.go @@ -435,7 +435,7 @@ func (m *StrIntMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *StrIntMap) MarshalJSON() ([]byte, error) { +func (m StrIntMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_hash_str_str_map.go b/container/gmap/gmap_hash_str_str_map.go index 1ffb2b527..d2c8a0bd1 100644 --- a/container/gmap/gmap_hash_str_str_map.go +++ b/container/gmap/gmap_hash_str_str_map.go @@ -434,7 +434,7 @@ func (m *StrStrMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *StrStrMap) MarshalJSON() ([]byte, error) { +func (m StrStrMap) MarshalJSON() ([]byte, error) { m.mu.RLock() defer m.mu.RUnlock() return json.Marshal(m.data) diff --git a/container/gmap/gmap_list_map.go b/container/gmap/gmap_list_map.go index 3ef696dfe..eed95f1a6 100644 --- a/container/gmap/gmap_list_map.go +++ b/container/gmap/gmap_list_map.go @@ -514,7 +514,7 @@ func (m *ListMap) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (m *ListMap) MarshalJSON() ([]byte, error) { +func (m ListMap) MarshalJSON() ([]byte, error) { return json.Marshal(gconv.Map(m.Map())) } diff --git a/container/gset/gset_any_set.go b/container/gset/gset_any_set.go index 9ab7da1ad..b04b05667 100644 --- a/container/gset/gset_any_set.go +++ b/container/gset/gset_any_set.go @@ -467,7 +467,7 @@ func (set *Set) Walk(f func(item interface{}) interface{}) *Set { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (set *Set) MarshalJSON() ([]byte, error) { +func (set Set) MarshalJSON() ([]byte, error) { return json.Marshal(set.Slice()) } diff --git a/container/gset/gset_int_set.go b/container/gset/gset_int_set.go index efc75bbff..cda8a6711 100644 --- a/container/gset/gset_int_set.go +++ b/container/gset/gset_int_set.go @@ -426,7 +426,7 @@ func (set *IntSet) Walk(f func(item int) int) *IntSet { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (set *IntSet) MarshalJSON() ([]byte, error) { +func (set IntSet) MarshalJSON() ([]byte, error) { return json.Marshal(set.Slice()) } diff --git a/container/gset/gset_str_set.go b/container/gset/gset_str_set.go index 42c57fd07..3d13de1e4 100644 --- a/container/gset/gset_str_set.go +++ b/container/gset/gset_str_set.go @@ -454,7 +454,7 @@ func (set *StrSet) Walk(f func(item string) string) *StrSet { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (set *StrSet) MarshalJSON() ([]byte, error) { +func (set StrSet) MarshalJSON() ([]byte, error) { return json.Marshal(set.Slice()) } diff --git a/container/gtree/gtree_avltree.go b/container/gtree/gtree_avltree.go index 421865dbe..23c9fa0c8 100644 --- a/container/gtree/gtree_avltree.go +++ b/container/gtree/gtree_avltree.go @@ -780,7 +780,7 @@ func output(node *AVLTreeNode, prefix string, isTail bool, str *string) { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (tree *AVLTree) MarshalJSON() ([]byte, error) { +func (tree AVLTree) MarshalJSON() ([]byte, error) { return json.Marshal(tree.MapStrAny()) } diff --git a/container/gtree/gtree_btree.go b/container/gtree/gtree_btree.go index fff430b3e..87f10ddbc 100644 --- a/container/gtree/gtree_btree.go +++ b/container/gtree/gtree_btree.go @@ -944,7 +944,7 @@ func (tree *BTree) deleteChild(node *BTreeNode, index int) { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (tree *BTree) MarshalJSON() ([]byte, error) { +func (tree BTree) MarshalJSON() ([]byte, error) { return json.Marshal(tree.MapStrAny()) } diff --git a/container/gtree/gtree_redblacktree.go b/container/gtree/gtree_redblacktree.go index 2433648a3..01d875dcc 100644 --- a/container/gtree/gtree_redblacktree.go +++ b/container/gtree/gtree_redblacktree.go @@ -925,7 +925,7 @@ func (tree *RedBlackTree) nodeColor(node *RedBlackTreeNode) color { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (tree *RedBlackTree) MarshalJSON() ([]byte, error) { +func (tree RedBlackTree) MarshalJSON() ([]byte, error) { return json.Marshal(gconv.Map(tree.Map())) } diff --git a/container/gtype/gtype_bool.go b/container/gtype/gtype_bool.go index d1b00e3dd..42d0c6a33 100644 --- a/container/gtype/gtype_bool.go +++ b/container/gtype/gtype_bool.go @@ -78,7 +78,7 @@ func (v *Bool) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Bool) MarshalJSON() ([]byte, error) { +func (v Bool) MarshalJSON() ([]byte, error) { if v.Val() { return bytesTrue, nil } diff --git a/container/gtype/gtype_byte.go b/container/gtype/gtype_byte.go index 5a8ffb385..87e41d29c 100644 --- a/container/gtype/gtype_byte.go +++ b/container/gtype/gtype_byte.go @@ -60,7 +60,7 @@ func (v *Byte) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Byte) MarshalJSON() ([]byte, error) { +func (v Byte) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } diff --git a/container/gtype/gtype_bytes.go b/container/gtype/gtype_bytes.go index 0f8e4095c..6b69febfe 100644 --- a/container/gtype/gtype_bytes.go +++ b/container/gtype/gtype_bytes.go @@ -57,7 +57,7 @@ func (v *Bytes) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Bytes) MarshalJSON() ([]byte, error) { +func (v Bytes) MarshalJSON() ([]byte, error) { val := v.Val() dst := make([]byte, base64.StdEncoding.EncodedLen(len(val))) base64.StdEncoding.Encode(dst, val) diff --git a/container/gtype/gtype_float32.go b/container/gtype/gtype_float32.go index fc096c8d7..961a822bc 100644 --- a/container/gtype/gtype_float32.go +++ b/container/gtype/gtype_float32.go @@ -72,7 +72,7 @@ func (v *Float32) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Float32) MarshalJSON() ([]byte, error) { +func (v Float32) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil } diff --git a/container/gtype/gtype_float64.go b/container/gtype/gtype_float64.go index fd5a2e99e..2fc869355 100644 --- a/container/gtype/gtype_float64.go +++ b/container/gtype/gtype_float64.go @@ -72,7 +72,7 @@ func (v *Float64) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Float64) MarshalJSON() ([]byte, error) { +func (v Float64) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil } diff --git a/container/gtype/gtype_int.go b/container/gtype/gtype_int.go index 9e6dbeaa6..2114b8822 100644 --- a/container/gtype/gtype_int.go +++ b/container/gtype/gtype_int.go @@ -60,7 +60,7 @@ func (v *Int) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Int) MarshalJSON() ([]byte, error) { +func (v Int) MarshalJSON() ([]byte, error) { return []byte(strconv.Itoa(v.Val())), nil } diff --git a/container/gtype/gtype_int32.go b/container/gtype/gtype_int32.go index 849f3dcd8..99a3188a0 100644 --- a/container/gtype/gtype_int32.go +++ b/container/gtype/gtype_int32.go @@ -60,7 +60,7 @@ func (v *Int32) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Int32) MarshalJSON() ([]byte, error) { +func (v Int32) MarshalJSON() ([]byte, error) { return []byte(strconv.Itoa(int(v.Val()))), nil } diff --git a/container/gtype/gtype_int64.go b/container/gtype/gtype_int64.go index 3bf12afef..85558aceb 100644 --- a/container/gtype/gtype_int64.go +++ b/container/gtype/gtype_int64.go @@ -60,7 +60,7 @@ func (v *Int64) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Int64) MarshalJSON() ([]byte, error) { +func (v Int64) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatInt(v.Val(), 10)), nil } diff --git a/container/gtype/gtype_interface.go b/container/gtype/gtype_interface.go index a9ad7877b..04f95d3c5 100644 --- a/container/gtype/gtype_interface.go +++ b/container/gtype/gtype_interface.go @@ -52,7 +52,7 @@ func (v *Interface) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Interface) MarshalJSON() ([]byte, error) { +func (v Interface) MarshalJSON() ([]byte, error) { return json.Marshal(v.Val()) } diff --git a/container/gtype/gtype_string.go b/container/gtype/gtype_string.go index e22f680b5..f3e442eb2 100644 --- a/container/gtype/gtype_string.go +++ b/container/gtype/gtype_string.go @@ -54,7 +54,7 @@ func (v *String) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *String) MarshalJSON() ([]byte, error) { +func (v String) MarshalJSON() ([]byte, error) { return []byte(`"` + v.Val() + `"`), nil } diff --git a/container/gtype/gtype_uint.go b/container/gtype/gtype_uint.go index e9dada149..763d90f6a 100644 --- a/container/gtype/gtype_uint.go +++ b/container/gtype/gtype_uint.go @@ -60,7 +60,7 @@ func (v *Uint) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Uint) MarshalJSON() ([]byte, error) { +func (v Uint) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } diff --git a/container/gtype/gtype_uint32.go b/container/gtype/gtype_uint32.go index 1e52377e3..b877b56c4 100644 --- a/container/gtype/gtype_uint32.go +++ b/container/gtype/gtype_uint32.go @@ -60,7 +60,7 @@ func (v *Uint32) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Uint32) MarshalJSON() ([]byte, error) { +func (v Uint32) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } diff --git a/container/gtype/gtype_uint64.go b/container/gtype/gtype_uint64.go index f7cda75bb..71a7bd202 100644 --- a/container/gtype/gtype_uint64.go +++ b/container/gtype/gtype_uint64.go @@ -60,7 +60,7 @@ func (v *Uint64) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Uint64) MarshalJSON() ([]byte, error) { +func (v Uint64) MarshalJSON() ([]byte, error) { return []byte(strconv.FormatUint(v.Val(), 10)), nil } diff --git a/container/gvar/gvar.go b/container/gvar/gvar.go index f9d79adab..f17afae55 100644 --- a/container/gvar/gvar.go +++ b/container/gvar/gvar.go @@ -169,7 +169,7 @@ func (v *Var) GTime(format ...string) *gtime.Time { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (v *Var) MarshalJSON() ([]byte, error) { +func (v Var) MarshalJSON() ([]byte, error) { return json.Marshal(v.Val()) } diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 18feebc59..594145c96 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -633,7 +633,7 @@ func (c *Core) DoDelete(ctx context.Context, link Link, table string, condition // // Note that this interface implements mainly for workaround for a json infinite loop bug // of Golang version < v1.14. -func (c *Core) MarshalJSON() ([]byte, error) { +func (c Core) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`%+v`, c)), nil } diff --git a/encoding/gjson/gjson_implements.go b/encoding/gjson/gjson_implements.go index a907e03f6..60e146885 100644 --- a/encoding/gjson/gjson_implements.go +++ b/encoding/gjson/gjson_implements.go @@ -7,7 +7,7 @@ package gjson // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (j *Json) MarshalJSON() ([]byte, error) { +func (j Json) MarshalJSON() ([]byte, error) { return j.ToJson() } diff --git a/errors/gerror/gerror_error.go b/errors/gerror/gerror_error.go index e0f27bb4d..c69f01836 100644 --- a/errors/gerror/gerror_error.go +++ b/errors/gerror/gerror_error.go @@ -185,7 +185,7 @@ func (err *Error) SetCode(code gcode.Code) { // MarshalJSON implements the interface MarshalJSON for json.Marshal. // Note that do not use pointer as its receiver here. -func (err *Error) MarshalJSON() ([]byte, error) { +func (err Error) MarshalJSON() ([]byte, error) { return []byte(`"` + err.Error() + `"`), nil } diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 8ae683d97..999dda6c7 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -220,7 +220,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (item *handlerItem) MarshalJSON() ([]byte, error) { +func (item handlerItem) MarshalJSON() ([]byte, error) { switch item.Type { case HandlerTypeHook: return json.Marshal( @@ -254,6 +254,6 @@ func (item *handlerItem) MarshalJSON() ([]byte, error) { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (item *handlerParsedItem) MarshalJSON() ([]byte, error) { +func (item handlerParsedItem) MarshalJSON() ([]byte, error) { return json.Marshal(item.Handler) } diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go index 1ed5f28eb..e9be2f129 100644 --- a/os/gcmd/gcmd_parser.go +++ b/os/gcmd/gcmd_parser.go @@ -221,7 +221,7 @@ func (p *Parser) GetArgAll() []string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (p *Parser) MarshalJSON() ([]byte, error) { +func (p Parser) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]interface{}{ "parsedArgs": p.parsedArgs, "parsedOptions": p.parsedOptions, diff --git a/os/gres/gres_file.go b/os/gres/gres_file.go index 036337c87..3a826a8cc 100644 --- a/os/gres/gres_file.go +++ b/os/gres/gres_file.go @@ -55,7 +55,7 @@ func (f *File) FileInfo() os.FileInfo { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (f *File) MarshalJSON() ([]byte, error) { +func (f File) MarshalJSON() ([]byte, error) { info := f.FileInfo() return json.Marshal(map[string]interface{}{ "name": f.Name(), diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index 0854968ed..1d6873aed 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -446,6 +446,8 @@ func (t *Time) EndOfYear() *Time { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. +// Note that, DO NOT use `(t *Time) MarshalJSON() ([]byte, error)` as it looses interface +// implement of `MarshalJSON` for struct of Time. func (t Time) MarshalJSON() ([]byte, error) { return []byte(`"` + t.String() + `"`), nil } diff --git a/os/gtime/gtime_z_unit_feature_json_test.go b/os/gtime/gtime_z_unit_feature_json_test.go index 1704ff926..e1e90e7ac 100644 --- a/os/gtime/gtime_z_unit_feature_json_test.go +++ b/os/gtime/gtime_z_unit_feature_json_test.go @@ -12,6 +12,7 @@ import ( "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/util/gconv" ) func Test_Json_Pointer(t *testing.T) { @@ -27,6 +28,21 @@ func Test_Json_Pointer(t *testing.T) { t.Assert(err, nil) t.Assert(j, `{"Time":"2006-01-02 15:04:05"}`) }) + // Marshal struct with embedded. + gtest.C(t, func(t *gtest.T) { + type Time struct { + MyTime *gtime.Time + } + type T struct { + Time + } + t1 := new(T) + s := "2006-01-02 15:04:05" + t1.MyTime = gtime.NewFromStr(s) + j, err := json.Marshal(gconv.Map(t1)) + t.Assert(err, nil) + t.Assert(j, `{"MyTime":"2006-01-02 15:04:05"}`) + }) // Marshal nil gtest.C(t, func(t *gtest.T) { type T struct {