diff --git a/g/container/garray/garray_normal_int.go b/g/container/garray/garray_normal_int.go index 339063ddf..ba7c34105 100644 --- a/g/container/garray/garray_normal_int.go +++ b/g/container/garray/garray_normal_int.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -616,3 +617,10 @@ func (a *IntArray) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *IntArray) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/garray/garray_normal_interface.go b/g/container/garray/garray_normal_interface.go index c91cd2d97..33d802c63 100644 --- a/g/container/garray/garray_normal_interface.go +++ b/g/container/garray/garray_normal_interface.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -610,3 +611,10 @@ func (a *Array) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *Array) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/garray/garray_normal_string.go b/g/container/garray/garray_normal_string.go index bfc61092e..8bf15c49e 100644 --- a/g/container/garray/garray_normal_string.go +++ b/g/container/garray/garray_normal_string.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -616,3 +617,10 @@ func (a *StringArray) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *StringArray) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 8fcd9a8e7..0662f94e1 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -541,3 +542,10 @@ func (a *SortedIntArray) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *SortedIntArray) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index ea2966fae..83a76e09d 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -542,3 +543,10 @@ func (a *SortedArray) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *SortedArray) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index 89e06b43d..b560fc532 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -8,6 +8,7 @@ package garray import ( "bytes" + "encoding/json" "fmt" "math" "sort" @@ -536,3 +537,10 @@ func (a *SortedStringArray) String() string { defer a.mu.RUnlock() return fmt.Sprint(a.array) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (a *SortedStringArray) MarshalJSON() ([]byte, error) { + a.mu.RLock() + defer a.mu.RUnlock() + return json.Marshal(a.array) +} diff --git a/g/container/gmap/gmap_hash_any_any_map.go b/g/container/gmap/gmap_hash_any_any_map.go index d2953c0eb..db751099c 100644 --- a/g/container/gmap/gmap_hash_any_any_map.go +++ b/g/container/gmap/gmap_hash_any_any_map.go @@ -7,6 +7,10 @@ package gmap import ( + "encoding/json" + + "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" ) @@ -328,3 +332,8 @@ func (m *AnyAnyMap) Merge(other *AnyAnyMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *AnyAnyMap) MarshalJSON() ([]byte, error) { + return json.Marshal(gconv.Map(m.Map())) +} diff --git a/g/container/gmap/gmap_hash_int_any_map.go b/g/container/gmap/gmap_hash_int_any_map.go index e805999df..1645fcc10 100644 --- a/g/container/gmap/gmap_hash_int_any_map.go +++ b/g/container/gmap/gmap_hash_int_any_map.go @@ -8,6 +8,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" "github.com/gogf/gf/g/util/gconv" @@ -330,3 +332,10 @@ func (m *IntAnyMap) Merge(other *IntAnyMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *IntAnyMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_hash_int_int_map.go b/g/container/gmap/gmap_hash_int_int_map.go index c01382f55..637989003 100644 --- a/g/container/gmap/gmap_hash_int_int_map.go +++ b/g/container/gmap/gmap_hash_int_int_map.go @@ -7,6 +7,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/internal/rwmutex" ) @@ -306,3 +308,10 @@ func (m *IntIntMap) Merge(other *IntIntMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *IntIntMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_hash_int_str_map.go b/g/container/gmap/gmap_hash_int_str_map.go index 944f6e134..225c370df 100644 --- a/g/container/gmap/gmap_hash_int_str_map.go +++ b/g/container/gmap/gmap_hash_int_str_map.go @@ -7,6 +7,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/internal/rwmutex" "github.com/gogf/gf/g/util/gconv" ) @@ -307,3 +309,10 @@ func (m *IntStrMap) Merge(other *IntStrMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *IntStrMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_hash_str_any_map.go b/g/container/gmap/gmap_hash_str_any_map.go index 452a82d3c..5e2b4d117 100644 --- a/g/container/gmap/gmap_hash_str_any_map.go +++ b/g/container/gmap/gmap_hash_str_any_map.go @@ -8,6 +8,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" "github.com/gogf/gf/g/util/gconv" @@ -332,3 +334,10 @@ func (m *StrAnyMap) Merge(other *StrAnyMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *StrAnyMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_hash_str_int_map.go b/g/container/gmap/gmap_hash_str_int_map.go index 34df9a979..df7e13b12 100644 --- a/g/container/gmap/gmap_hash_str_int_map.go +++ b/g/container/gmap/gmap_hash_str_int_map.go @@ -8,6 +8,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/internal/rwmutex" "github.com/gogf/gf/g/util/gconv" ) @@ -310,3 +312,10 @@ func (m *StrIntMap) Merge(other *StrIntMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *StrIntMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_hash_str_str_map.go b/g/container/gmap/gmap_hash_str_str_map.go index 373d9fa48..ef03174b3 100644 --- a/g/container/gmap/gmap_hash_str_str_map.go +++ b/g/container/gmap/gmap_hash_str_str_map.go @@ -8,6 +8,8 @@ package gmap import ( + "encoding/json" + "github.com/gogf/gf/g/internal/rwmutex" ) @@ -309,3 +311,10 @@ func (m *StrStrMap) Merge(other *StrStrMap) { m.data[k] = v } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *StrStrMap) MarshalJSON() ([]byte, error) { + m.mu.RLock() + defer m.mu.RUnlock() + return json.Marshal(m.data) +} diff --git a/g/container/gmap/gmap_link_map.go b/g/container/gmap/gmap_link_map.go index 177385f03..36e61f2c6 100644 --- a/g/container/gmap/gmap_link_map.go +++ b/g/container/gmap/gmap_link_map.go @@ -7,6 +7,10 @@ package gmap import ( + "encoding/json" + + "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/container/glist" "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" @@ -364,3 +368,8 @@ func (m *ListMap) Merge(other *ListMap) { return true }) } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (m *ListMap) MarshalJSON() ([]byte, error) { + return json.Marshal(gconv.Map(m.Map())) +} diff --git a/g/container/gtree/gtree_avltree.go b/g/container/gtree/gtree_avltree.go index dece7f1d5..e8bd607ad 100644 --- a/g/container/gtree/gtree_avltree.go +++ b/g/container/gtree/gtree_avltree.go @@ -7,7 +7,9 @@ package gtree import ( + "encoding/json" "fmt" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" ) @@ -704,3 +706,8 @@ func output(node *AVLTreeNode, prefix string, isTail bool, str *string) { output(node.children[0], newPrefix, true, str) } } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (tree *AVLTree) MarshalJSON() ([]byte, error) { + return json.Marshal(tree.Map()) +} diff --git a/g/container/gtree/gtree_btree.go b/g/container/gtree/gtree_btree.go index d7d0cc014..799ab0294 100644 --- a/g/container/gtree/gtree_btree.go +++ b/g/container/gtree/gtree_btree.go @@ -8,10 +8,12 @@ package gtree import ( "bytes" + "encoding/json" "fmt" + "strings" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" - "strings" ) // BTree holds elements of the B-tree. @@ -845,3 +847,8 @@ func (tree *BTree) deleteChild(node *BTreeNode, index int) { node.Children[len(node.Children)-1] = nil node.Children = node.Children[:len(node.Children)-1] } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (tree *BTree) MarshalJSON() ([]byte, error) { + return json.Marshal(tree.Map()) +} diff --git a/g/container/gtree/gtree_redblacktree.go b/g/container/gtree/gtree_redblacktree.go index 13891edb8..4bfe76a08 100644 --- a/g/container/gtree/gtree_redblacktree.go +++ b/g/container/gtree/gtree_redblacktree.go @@ -7,7 +7,9 @@ package gtree import ( + "encoding/json" "fmt" + "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/internal/rwmutex" ) @@ -833,3 +835,8 @@ func (tree *RedBlackTree) nodeColor(node *RedBlackTreeNode) color { } return node.color } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (tree *RedBlackTree) MarshalJSON() ([]byte, error) { + return json.Marshal(tree.Map()) +} diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go index bbf3543d9..acc554414 100644 --- a/g/container/gvar/gvar.go +++ b/g/container/gvar/gvar.go @@ -8,10 +8,12 @@ package gvar import ( + "encoding/json" + "time" + "github.com/gogf/gf/g/container/gtype" "github.com/gogf/gf/g/os/gtime" "github.com/gogf/gf/g/util/gconv" - "time" ) // Var is an universal variable type. @@ -34,6 +36,11 @@ func New(value interface{}, unsafe ...bool) *Var { return v } +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (v *Var) MarshalJSON() ([]byte, error) { + return json.Marshal(v.Val()) +} + // Set sets to , and returns the old value. func (v *Var) Set(value interface{}) (old interface{}) { if v.safe { diff --git a/g/os/gtime/gtime_time.go b/g/os/gtime/gtime_time.go index 675d3d88a..1d7efee29 100644 --- a/g/os/gtime/gtime_time.go +++ b/g/os/gtime/gtime_time.go @@ -6,7 +6,9 @@ package gtime -import "time" +import ( + "time" +) type Time struct { time.Time @@ -175,3 +177,8 @@ func (t *Time) Truncate(d time.Duration) *Time { t.Time = t.Time.Truncate(d) return t } + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (t *Time) MarshalJSON() ([]byte, error) { + return []byte(`"` + t.String() + `"`), nil +} diff --git a/geg/container/gmap/gmap_json.go b/geg/container/gmap/gmap_json.go new file mode 100644 index 000000000..233e204aa --- /dev/null +++ b/geg/container/gmap/gmap_json.go @@ -0,0 +1,24 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/gogf/gf/g/container/gmap" +) + +func main() { + //m := gmap.New() + //m.Set("k", "v") + //m.Set("1", "2") + //b, err := json.Marshal(m) + //fmt.Println(err) + //fmt.Println(string(b)) + + m := gmap.NewIntIntMap() + m.Set(1, 2) + m.Set(3, 4) + b, err := json.Marshal(m) + fmt.Println(err) + fmt.Println(string(b)) +} diff --git a/geg/os/gtime/gtime_json.go b/geg/os/gtime/gtime_json.go new file mode 100644 index 000000000..6c7e37906 --- /dev/null +++ b/geg/os/gtime/gtime_json.go @@ -0,0 +1,15 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/gogf/gf/g/os/gtime" +) + +func main() { + t := gtime.Now() + b, err := json.Marshal(t) + fmt.Println(err) + fmt.Println(string(b)) +}