change pointer receiver that implements interface MarshalJSON to struct receiver for all packages

This commit is contained in:
John Guo 2022-01-19 16:55:57 +08:00
parent d7b94428ae
commit 3bbbe1db9c
37 changed files with 54 additions and 36 deletions

View File

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

View File

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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

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

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

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

View File

@ -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
}

View File

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

View File

@ -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
}

View File

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

View File

@ -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,

View File

@ -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(),

View File

@ -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
}

View File

@ -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 {