From c9a2dcad62ad67eae246af2ebe23b5aff84e15cf Mon Sep 17 00:00:00 2001 From: mshadow Date: Thu, 8 Apr 2021 15:19:55 +0800 Subject: [PATCH] Implements json.Marshaler and json.Unmarshaler --- lists/arraylist/serialization.go | 12 ++++++++++++ lists/doublylinkedlist/serialization.go | 12 ++++++++++++ lists/singlylinkedlist/serialization.go | 12 ++++++++++++ maps/hashbidimap/serialization.go | 12 ++++++++++++ maps/hashmap/serialization.go | 12 ++++++++++++ maps/linkedhashmap/serialization.go | 12 ++++++++++++ maps/treebidimap/serialization.go | 12 ++++++++++++ maps/treemap/serialization.go | 18 +++++++++++++++++- sets/hashset/serialization.go | 12 ++++++++++++ sets/linkedhashset/serialization.go | 12 ++++++++++++ sets/treeset/serialization.go | 12 ++++++++++++ stacks/arraystack/serialization.go | 18 +++++++++++++++++- stacks/linkedliststack/serialization.go | 18 +++++++++++++++++- trees/avltree/serialization.go | 12 ++++++++++++ trees/binaryheap/serialization.go | 18 +++++++++++++++++- trees/btree/serialization.go | 12 ++++++++++++ trees/redblacktree/serialization.go | 12 ++++++++++++ 17 files changed, 224 insertions(+), 4 deletions(-) diff --git a/lists/arraylist/serialization.go b/lists/arraylist/serialization.go index 2f283fb..eeaaa4d 100644 --- a/lists/arraylist/serialization.go +++ b/lists/arraylist/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONDeserializer = (*List)(nil) + var _ json.Marshaler = (*List)(nil) + var _ json.Unmarshaler = (*List)(nil) } // ToJSON outputs the JSON representation of list's elements. @@ -27,3 +29,13 @@ func (list *List) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (list *List) UnmarshalJSON(bytes []byte) error { + return list.FromJSON(bytes) +} + +// @implements json.Marshaler +func (list *List) MarshalJSON() ([]byte, error) { + return list.ToJSON() +} diff --git a/lists/doublylinkedlist/serialization.go b/lists/doublylinkedlist/serialization.go index 6018d80..a61d9ba 100644 --- a/lists/doublylinkedlist/serialization.go +++ b/lists/doublylinkedlist/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONDeserializer = (*List)(nil) + var _ json.Marshaler = (*List)(nil) + var _ json.Unmarshaler = (*List)(nil) } // ToJSON outputs the JSON representation of list's elements. @@ -29,3 +31,13 @@ func (list *List) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (list *List) UnmarshalJSON(bytes []byte) error { + return list.FromJSON(bytes) +} + +// @implements json.Marshaler +func (list *List) MarshalJSON() ([]byte, error) { + return list.ToJSON() +} diff --git a/lists/singlylinkedlist/serialization.go b/lists/singlylinkedlist/serialization.go index 324f1d9..74ce75f 100644 --- a/lists/singlylinkedlist/serialization.go +++ b/lists/singlylinkedlist/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONDeserializer = (*List)(nil) + var _ json.Marshaler = (*List)(nil) + var _ json.Unmarshaler = (*List)(nil) } // ToJSON outputs the JSON representation of list's elements. @@ -29,3 +31,13 @@ func (list *List) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (list *List) UnmarshalJSON(bytes []byte) error { + return list.FromJSON(bytes) +} + +// @implements json.Marshaler +func (list *List) MarshalJSON() ([]byte, error) { + return list.ToJSON() +} diff --git a/maps/hashbidimap/serialization.go b/maps/hashbidimap/serialization.go index 3db41d4..e05c4e8 100644 --- a/maps/hashbidimap/serialization.go +++ b/maps/hashbidimap/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil) + var _ json.Marshaler = (*Map)(nil) + var _ json.Unmarshaler = (*Map)(nil) } // ToJSON outputs the JSON representation of the map. @@ -31,3 +33,13 @@ func (m *Map) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (m *Map) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// @implements json.Marshaler +func (m *Map) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/maps/hashmap/serialization.go b/maps/hashmap/serialization.go index b06eb7e..8fe78fa 100644 --- a/maps/hashmap/serialization.go +++ b/maps/hashmap/serialization.go @@ -13,6 +13,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil) + var _ json.Marshaler = (*Map)(nil) + var _ json.Unmarshaler = (*Map)(nil) } // ToJSON outputs the JSON representation of the map. @@ -36,3 +38,13 @@ func (m *Map) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (m *Map) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// @implements json.Marshaler +func (m *Map) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/maps/linkedhashmap/serialization.go b/maps/linkedhashmap/serialization.go index 4f723cf..a628b91 100644 --- a/maps/linkedhashmap/serialization.go +++ b/maps/linkedhashmap/serialization.go @@ -14,6 +14,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil) + var _ json.Marshaler = (*Map)(nil) + var _ json.Unmarshaler = (*Map)(nil) } // ToJSON outputs the JSON representation of map. @@ -101,3 +103,13 @@ func (m *Map) FromJSON(data []byte) error { return nil } + +// @implements json.Unmarshaler +func (m *Map) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// @implements json.Marshaler +func (m *Map) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/maps/treebidimap/serialization.go b/maps/treebidimap/serialization.go index 17204f9..488d2a0 100644 --- a/maps/treebidimap/serialization.go +++ b/maps/treebidimap/serialization.go @@ -13,6 +13,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil) + var _ json.Marshaler = (*Map)(nil) + var _ json.Unmarshaler = (*Map)(nil) } // ToJSON outputs the JSON representation of the map. @@ -37,3 +39,13 @@ func (m *Map) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (m *Map) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// @implements json.Marshaler +func (m *Map) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/maps/treemap/serialization.go b/maps/treemap/serialization.go index d856300..d6b2d10 100644 --- a/maps/treemap/serialization.go +++ b/maps/treemap/serialization.go @@ -4,11 +4,17 @@ package treemap -import "github.com/emirpasic/gods/containers" +import ( + "encoding/json" + + "github.com/emirpasic/gods/containers" +) func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil) + var _ json.Marshaler = (*Map)(nil) + var _ json.Unmarshaler = (*Map)(nil) } // ToJSON outputs the JSON representation of the map. @@ -20,3 +26,13 @@ func (m *Map) ToJSON() ([]byte, error) { func (m *Map) FromJSON(data []byte) error { return m.tree.FromJSON(data) } + +// @implements json.Unmarshaler +func (m *Map) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// @implements json.Marshaler +func (m *Map) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/sets/hashset/serialization.go b/sets/hashset/serialization.go index 7b8506d..f93b0cb 100644 --- a/sets/hashset/serialization.go +++ b/sets/hashset/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONDeserializer = (*Set)(nil) + var _ json.Marshaler = (*Set)(nil) + var _ json.Unmarshaler = (*Set)(nil) } // ToJSON outputs the JSON representation of the set. @@ -29,3 +31,13 @@ func (set *Set) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (set *Set) UnmarshalJSON(bytes []byte) error { + return set.FromJSON(bytes) +} + +// @implements json.Marshaler +func (set *Set) MarshalJSON() ([]byte, error) { + return set.ToJSON() +} diff --git a/sets/linkedhashset/serialization.go b/sets/linkedhashset/serialization.go index 7e7d291..4e84be9 100644 --- a/sets/linkedhashset/serialization.go +++ b/sets/linkedhashset/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONDeserializer = (*Set)(nil) + var _ json.Marshaler = (*Set)(nil) + var _ json.Unmarshaler = (*Set)(nil) } // ToJSON outputs the JSON representation of the set. @@ -29,3 +31,13 @@ func (set *Set) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (set *Set) UnmarshalJSON(bytes []byte) error { + return set.FromJSON(bytes) +} + +// @implements json.Marshaler +func (set *Set) MarshalJSON() ([]byte, error) { + return set.ToJSON() +} diff --git a/sets/treeset/serialization.go b/sets/treeset/serialization.go index a53bfcc..a74ad6d 100644 --- a/sets/treeset/serialization.go +++ b/sets/treeset/serialization.go @@ -12,6 +12,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONDeserializer = (*Set)(nil) + var _ json.Marshaler = (*Set)(nil) + var _ json.Unmarshaler = (*Set)(nil) } // ToJSON outputs the JSON representation of the set. @@ -29,3 +31,13 @@ func (set *Set) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (set *Set) UnmarshalJSON(bytes []byte) error { + return set.FromJSON(bytes) +} + +// @implements json.Marshaler +func (set *Set) MarshalJSON() ([]byte, error) { + return set.ToJSON() +} diff --git a/stacks/arraystack/serialization.go b/stacks/arraystack/serialization.go index c4ff549..6d26fa7 100644 --- a/stacks/arraystack/serialization.go +++ b/stacks/arraystack/serialization.go @@ -4,11 +4,17 @@ package arraystack -import "github.com/emirpasic/gods/containers" +import ( + "encoding/json" + + "github.com/emirpasic/gods/containers" +) func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Stack)(nil) var _ containers.JSONDeserializer = (*Stack)(nil) + var _ json.Marshaler = (*Stack)(nil) + var _ json.Unmarshaler = (*Stack)(nil) } // ToJSON outputs the JSON representation of the stack. @@ -20,3 +26,13 @@ func (stack *Stack) ToJSON() ([]byte, error) { func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } + +// @implements json.Unmarshaler +func (stack *Stack) UnmarshalJSON(bytes []byte) error { + return stack.FromJSON(bytes) +} + +// @implements json.Marshaler +func (stack *Stack) MarshalJSON() ([]byte, error) { + return stack.ToJSON() +} diff --git a/stacks/linkedliststack/serialization.go b/stacks/linkedliststack/serialization.go index 63337a1..fce1511 100644 --- a/stacks/linkedliststack/serialization.go +++ b/stacks/linkedliststack/serialization.go @@ -4,11 +4,17 @@ package linkedliststack -import "github.com/emirpasic/gods/containers" +import ( + "encoding/json" + + "github.com/emirpasic/gods/containers" +) func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Stack)(nil) var _ containers.JSONDeserializer = (*Stack)(nil) + var _ json.Marshaler = (*Stack)(nil) + var _ json.Unmarshaler = (*Stack)(nil) } // ToJSON outputs the JSON representation of the stack. @@ -20,3 +26,13 @@ func (stack *Stack) ToJSON() ([]byte, error) { func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } + +// @implements json.Unmarshaler +func (stack *Stack) UnmarshalJSON(bytes []byte) error { + return stack.FromJSON(bytes) +} + +// @implements json.Marshaler +func (stack *Stack) MarshalJSON() ([]byte, error) { + return stack.ToJSON() +} diff --git a/trees/avltree/serialization.go b/trees/avltree/serialization.go index 363de7f..79d4e5e 100644 --- a/trees/avltree/serialization.go +++ b/trees/avltree/serialization.go @@ -13,6 +13,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONDeserializer = (*Tree)(nil) + var _ json.Marshaler = (*Tree)(nil) + var _ json.Unmarshaler = (*Tree)(nil) } // ToJSON outputs the JSON representation of the tree. @@ -37,3 +39,13 @@ func (tree *Tree) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (tree *Tree) UnmarshalJSON(bytes []byte) error { + return tree.FromJSON(bytes) +} + +// @implements json.Marshaler +func (tree *Tree) MarshalJSON() ([]byte, error) { + return tree.ToJSON() +} diff --git a/trees/binaryheap/serialization.go b/trees/binaryheap/serialization.go index 00d0c77..e0d2a93 100644 --- a/trees/binaryheap/serialization.go +++ b/trees/binaryheap/serialization.go @@ -4,11 +4,17 @@ package binaryheap -import "github.com/emirpasic/gods/containers" +import ( + "encoding/json" + + "github.com/emirpasic/gods/containers" +) func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Heap)(nil) var _ containers.JSONDeserializer = (*Heap)(nil) + var _ json.Marshaler = (*Heap)(nil) + var _ json.Unmarshaler = (*Heap)(nil) } // ToJSON outputs the JSON representation of the heap. @@ -20,3 +26,13 @@ func (heap *Heap) ToJSON() ([]byte, error) { func (heap *Heap) FromJSON(data []byte) error { return heap.list.FromJSON(data) } + +// @implements json.Unmarshaler +func (heap *Heap) UnmarshalJSON(bytes []byte) error { + return heap.FromJSON(bytes) +} + +// @implements json.Marshaler +func (heap *Heap) MarshalJSON() ([]byte, error) { + return heap.ToJSON() +} diff --git a/trees/btree/serialization.go b/trees/btree/serialization.go index 4385167..75ef728 100644 --- a/trees/btree/serialization.go +++ b/trees/btree/serialization.go @@ -13,6 +13,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONDeserializer = (*Tree)(nil) + var _ json.Marshaler = (*Tree)(nil) + var _ json.Unmarshaler = (*Tree)(nil) } // ToJSON outputs the JSON representation of the tree. @@ -37,3 +39,13 @@ func (tree *Tree) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (tree *Tree) UnmarshalJSON(bytes []byte) error { + return tree.FromJSON(bytes) +} + +// @implements json.Marshaler +func (tree *Tree) MarshalJSON() ([]byte, error) { + return tree.ToJSON() +} diff --git a/trees/redblacktree/serialization.go b/trees/redblacktree/serialization.go index a1b8a77..84c4821 100644 --- a/trees/redblacktree/serialization.go +++ b/trees/redblacktree/serialization.go @@ -13,6 +13,8 @@ import ( func assertSerializationImplementation() { var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONDeserializer = (*Tree)(nil) + var _ json.Marshaler = (*Tree)(nil) + var _ json.Unmarshaler = (*Tree)(nil) } // ToJSON outputs the JSON representation of the tree. @@ -37,3 +39,13 @@ func (tree *Tree) FromJSON(data []byte) error { } return err } + +// @implements json.Unmarshaler +func (tree *Tree) UnmarshalJSON(bytes []byte) error { + return tree.FromJSON(bytes) +} + +// @implements json.Marshaler +func (tree *Tree) MarshalJSON() ([]byte, error) { + return tree.ToJSON() +}