From 1f0b87f0e17d460400ba4ab1f1b9f172ef39bafa Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Tue, 12 Apr 2022 04:31:44 +0200 Subject: [PATCH] Implements json.Marshaler and json.Unmarshaler interfaces --- containers/serialization.go | 4 ++++ lists/arraylist/arraylist_test.go | 10 ++++++++-- lists/arraylist/serialization.go | 6 ++---- lists/doublylinkedlist/doublylinkedlist_test.go | 10 ++++++++-- lists/doublylinkedlist/serialization.go | 6 ++---- lists/singlylinkedlist/serialization.go | 6 ++---- lists/singlylinkedlist/singlylinkedlist_test.go | 10 ++++++++-- maps/hashbidimap/hashbidimap_test.go | 10 ++++++++-- maps/hashbidimap/serialization.go | 6 ++---- maps/hashmap/hashmap_test.go | 10 ++++++++-- maps/hashmap/serialization.go | 6 ++---- maps/linkedhashmap/linkedhashmap_test.go | 11 +++++++++++ maps/linkedhashmap/serialization.go | 6 ++---- maps/treebidimap/serialization.go | 6 ++---- maps/treebidimap/treebidimap_test.go | 11 +++++++++++ maps/treemap/serialization.go | 8 ++------ maps/treemap/treemap_test.go | 11 +++++++++++ sets/hashset/hashset_test.go | 10 ++++++++-- sets/hashset/serialization.go | 6 ++---- sets/linkedhashset/linkedhashset_test.go | 10 ++++++++-- sets/linkedhashset/serialization.go | 6 ++---- sets/treeset/serialization.go | 6 ++---- sets/treeset/treeset_test.go | 10 ++++++++-- stacks/arraystack/arraystack_test.go | 10 ++++++++-- stacks/arraystack/serialization.go | 8 ++------ stacks/linkedliststack/linkedliststack_test.go | 10 ++++++++-- stacks/linkedliststack/serialization.go | 8 ++------ trees/avltree/avltree_test.go | 10 ++++++++-- trees/avltree/serialization.go | 6 ++---- trees/binaryheap/binaryheap_test.go | 10 ++++++++-- trees/binaryheap/serialization.go | 8 ++------ trees/btree/btree_test.go | 10 ++++++++-- trees/btree/serialization.go | 6 ++---- trees/redblacktree/redblacktree_test.go | 10 ++++++++-- trees/redblacktree/serialization.go | 6 ++---- 35 files changed, 183 insertions(+), 104 deletions(-) diff --git a/containers/serialization.go b/containers/serialization.go index d7c90c83..fd9cbe23 100644 --- a/containers/serialization.go +++ b/containers/serialization.go @@ -8,10 +8,14 @@ package containers type JSONSerializer interface { // ToJSON outputs the JSON representation of containers's elements. ToJSON() ([]byte, error) + // MarshalJSON @implements json.Marshaler + MarshalJSON() ([]byte, error) } // JSONDeserializer provides JSON deserialization type JSONDeserializer interface { // FromJSON populates containers's elements from the input JSON representation. FromJSON([]byte) error + // UnmarshalJSON @implements json.Unmarshaler + UnmarshalJSON([]byte) error } diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 88d7689c..2a704d17 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -5,6 +5,7 @@ package arraylist import ( + "encoding/json" "fmt" "github.com/emirpasic/gods/utils" "strings" @@ -620,11 +621,16 @@ func TestListSerialization(t *testing.T) { assert() - json, err := list.ToJSON() + bytes, err := list.ToJSON() assert() - err = list.FromJSON(json) + err = list.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", list}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/lists/arraylist/serialization.go b/lists/arraylist/serialization.go index eeaaa4d2..402298f0 100644 --- a/lists/arraylist/serialization.go +++ b/lists/arraylist/serialization.go @@ -12,8 +12,6 @@ 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. @@ -30,12 +28,12 @@ func (list *List) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (list *List) UnmarshalJSON(bytes []byte) error { return list.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (list *List) MarshalJSON() ([]byte, error) { return list.ToJSON() } diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 2e6b0c92..911da774 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -5,6 +5,7 @@ package doublylinkedlist import ( + "encoding/json" "fmt" "strings" "testing" @@ -626,11 +627,16 @@ func TestListSerialization(t *testing.T) { assert() - json, err := list.ToJSON() + bytes, err := list.ToJSON() assert() - err = list.FromJSON(json) + err = list.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", list}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/lists/doublylinkedlist/serialization.go b/lists/doublylinkedlist/serialization.go index a61d9ba6..ce0fb019 100644 --- a/lists/doublylinkedlist/serialization.go +++ b/lists/doublylinkedlist/serialization.go @@ -12,8 +12,6 @@ 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. @@ -32,12 +30,12 @@ func (list *List) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (list *List) UnmarshalJSON(bytes []byte) error { return list.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @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 74ce75ff..adcc5756 100644 --- a/lists/singlylinkedlist/serialization.go +++ b/lists/singlylinkedlist/serialization.go @@ -12,8 +12,6 @@ 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. @@ -32,12 +30,12 @@ func (list *List) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (list *List) UnmarshalJSON(bytes []byte) error { return list.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (list *List) MarshalJSON() ([]byte, error) { return list.ToJSON() } diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 3023a8ee..97eb78bb 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -5,6 +5,7 @@ package singlylinkedlist import ( + "encoding/json" "fmt" "strings" "testing" @@ -489,11 +490,16 @@ func TestListSerialization(t *testing.T) { assert() - json, err := list.ToJSON() + bytes, err := list.ToJSON() assert() - err = list.FromJSON(json) + err = list.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", list}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/maps/hashbidimap/hashbidimap_test.go b/maps/hashbidimap/hashbidimap_test.go index 96e46889..8b607f44 100644 --- a/maps/hashbidimap/hashbidimap_test.go +++ b/maps/hashbidimap/hashbidimap_test.go @@ -5,6 +5,7 @@ package hashbidimap import ( + "encoding/json" "fmt" "testing" ) @@ -174,11 +175,16 @@ func TestMapSerialization(t *testing.T) { assert() - json, err := m.ToJSON() + bytes, err := m.ToJSON() assert() - err = m.FromJSON(json) + err = m.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", m}) + if err != nil { + t.Errorf("Got error %v", err) + } } func sameElements(a []interface{}, b []interface{}) bool { diff --git a/maps/hashbidimap/serialization.go b/maps/hashbidimap/serialization.go index e05c4e8a..dff78017 100644 --- a/maps/hashbidimap/serialization.go +++ b/maps/hashbidimap/serialization.go @@ -12,8 +12,6 @@ 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. @@ -34,12 +32,12 @@ func (m *Map) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (m *Map) UnmarshalJSON(bytes []byte) error { return m.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (m *Map) MarshalJSON() ([]byte, error) { return m.ToJSON() } diff --git a/maps/hashmap/hashmap_test.go b/maps/hashmap/hashmap_test.go index 7122574d..14966806 100644 --- a/maps/hashmap/hashmap_test.go +++ b/maps/hashmap/hashmap_test.go @@ -5,6 +5,7 @@ package hashmap import ( + "encoding/json" "fmt" "testing" ) @@ -142,11 +143,16 @@ func TestMapSerialization(t *testing.T) { assert() - json, err := m.ToJSON() + bytes, err := m.ToJSON() assert() - err = m.FromJSON(json) + err = m.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", m}) + if err != nil { + t.Errorf("Got error %v", err) + } } func sameElements(a []interface{}, b []interface{}) bool { diff --git a/maps/hashmap/serialization.go b/maps/hashmap/serialization.go index 8fe78fa9..dc2e2598 100644 --- a/maps/hashmap/serialization.go +++ b/maps/hashmap/serialization.go @@ -13,8 +13,6 @@ 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. @@ -39,12 +37,12 @@ func (m *Map) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (m *Map) UnmarshalJSON(bytes []byte) error { return m.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (m *Map) MarshalJSON() ([]byte, error) { return m.ToJSON() } diff --git a/maps/linkedhashmap/linkedhashmap_test.go b/maps/linkedhashmap/linkedhashmap_test.go index 912e7f31..ac7b9940 100644 --- a/maps/linkedhashmap/linkedhashmap_test.go +++ b/maps/linkedhashmap/linkedhashmap_test.go @@ -5,6 +5,7 @@ package linkedhashmap import ( + "encoding/json" "fmt" "strings" "testing" @@ -571,6 +572,16 @@ func TestMapSerialization(t *testing.T) { } assertSerialization(deserialized, "C", t) } + + m := New() + m.Put("a", 1.0) + m.Put("b", 2.0) + m.Put("c", 3.0) + + _, err := json.Marshal([]interface{}{"a", "b", "c", m}) + if err != nil { + t.Errorf("Got error %v", err) + } } //noinspection GoBoolExpressions diff --git a/maps/linkedhashmap/serialization.go b/maps/linkedhashmap/serialization.go index a628b914..805421d3 100644 --- a/maps/linkedhashmap/serialization.go +++ b/maps/linkedhashmap/serialization.go @@ -14,8 +14,6 @@ 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. @@ -104,12 +102,12 @@ func (m *Map) FromJSON(data []byte) error { return nil } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (m *Map) UnmarshalJSON(bytes []byte) error { return m.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @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 488d2a0f..e7ee0c20 100644 --- a/maps/treebidimap/serialization.go +++ b/maps/treebidimap/serialization.go @@ -13,8 +13,6 @@ 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. @@ -40,12 +38,12 @@ func (m *Map) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (m *Map) UnmarshalJSON(bytes []byte) error { return m.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (m *Map) MarshalJSON() ([]byte, error) { return m.ToJSON() } diff --git a/maps/treebidimap/treebidimap_test.go b/maps/treebidimap/treebidimap_test.go index e1150fcc..acd32700 100644 --- a/maps/treebidimap/treebidimap_test.go +++ b/maps/treebidimap/treebidimap_test.go @@ -5,6 +5,7 @@ package treebidimap import ( + "encoding/json" "fmt" "github.com/emirpasic/gods/utils" "strings" @@ -604,6 +605,16 @@ func TestMapSerialization(t *testing.T) { } assertSerialization(deserialized, "C", t) } + + m := NewWith(utils.StringComparator, utils.Float64Comparator) + m.Put("a", 1.0) + m.Put("b", 2.0) + m.Put("c", 3.0) + + _, err := json.Marshal([]interface{}{"a", "b", "c", m}) + if err != nil { + t.Errorf("Got error %v", err) + } } //noinspection GoBoolExpressions diff --git a/maps/treemap/serialization.go b/maps/treemap/serialization.go index d6b2d101..619a6a77 100644 --- a/maps/treemap/serialization.go +++ b/maps/treemap/serialization.go @@ -5,16 +5,12 @@ package treemap 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. @@ -27,12 +23,12 @@ func (m *Map) FromJSON(data []byte) error { return m.tree.FromJSON(data) } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (m *Map) UnmarshalJSON(bytes []byte) error { return m.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (m *Map) MarshalJSON() ([]byte, error) { return m.ToJSON() } diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 05155a21..92e96124 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -5,6 +5,7 @@ package treemap import ( + "encoding/json" "fmt" "strings" "testing" @@ -627,6 +628,16 @@ func TestMapSerialization(t *testing.T) { } assertSerialization(deserialized, "C", t) } + + m := NewWithStringComparator() + m.Put("a", 1.0) + m.Put("b", 2.0) + m.Put("c", 3.0) + + _, err := json.Marshal([]interface{}{"a", "b", "c", m}) + if err != nil { + t.Errorf("Got error %v", err) + } } //noinspection GoBoolExpressions diff --git a/sets/hashset/hashset_test.go b/sets/hashset/hashset_test.go index cf63c899..4351338b 100644 --- a/sets/hashset/hashset_test.go +++ b/sets/hashset/hashset_test.go @@ -5,6 +5,7 @@ package hashset import ( + "encoding/json" "testing" ) @@ -98,11 +99,16 @@ func TestSetSerialization(t *testing.T) { assert() - json, err := set.ToJSON() + bytes, err := set.ToJSON() assert() - err = set.FromJSON(json) + err = set.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", set}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkContains(b *testing.B, set *Set, size int) { diff --git a/sets/hashset/serialization.go b/sets/hashset/serialization.go index f93b0cbc..b0353e83 100644 --- a/sets/hashset/serialization.go +++ b/sets/hashset/serialization.go @@ -12,8 +12,6 @@ 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. @@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (set *Set) UnmarshalJSON(bytes []byte) error { return set.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (set *Set) MarshalJSON() ([]byte, error) { return set.ToJSON() } diff --git a/sets/linkedhashset/linkedhashset_test.go b/sets/linkedhashset/linkedhashset_test.go index b77e5ecd..59db9ad9 100644 --- a/sets/linkedhashset/linkedhashset_test.go +++ b/sets/linkedhashset/linkedhashset_test.go @@ -5,6 +5,7 @@ package linkedhashset import ( + "encoding/json" "fmt" "strings" "testing" @@ -452,11 +453,16 @@ func TestSetSerialization(t *testing.T) { assert() - json, err := set.ToJSON() + bytes, err := set.ToJSON() assert() - err = set.FromJSON(json) + err = set.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", set}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkContains(b *testing.B, set *Set, size int) { diff --git a/sets/linkedhashset/serialization.go b/sets/linkedhashset/serialization.go index 4e84be9e..33b3fefd 100644 --- a/sets/linkedhashset/serialization.go +++ b/sets/linkedhashset/serialization.go @@ -12,8 +12,6 @@ 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. @@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (set *Set) UnmarshalJSON(bytes []byte) error { return set.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @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 a74ad6da..a17eb780 100644 --- a/sets/treeset/serialization.go +++ b/sets/treeset/serialization.go @@ -12,8 +12,6 @@ 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. @@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (set *Set) UnmarshalJSON(bytes []byte) error { return set.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (set *Set) MarshalJSON() ([]byte, error) { return set.ToJSON() } diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index 85718873..20a6f6a3 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -5,6 +5,7 @@ package treeset import ( + "encoding/json" "fmt" "strings" "testing" @@ -461,11 +462,16 @@ func TestSetSerialization(t *testing.T) { assert() - json, err := set.ToJSON() + bytes, err := set.ToJSON() assert() - err = set.FromJSON(json) + err = set.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", set}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkContains(b *testing.B, set *Set, size int) { diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index 0596217b..d15e7eb0 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -5,6 +5,7 @@ package arraystack import ( + "encoding/json" "fmt" "strings" "testing" @@ -360,11 +361,16 @@ func TestStackSerialization(t *testing.T) { assert() - json, err := stack.ToJSON() + bytes, err := stack.ToJSON() assert() - err = stack.FromJSON(json) + err = stack.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", stack}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkPush(b *testing.B, stack *Stack, size int) { diff --git a/stacks/arraystack/serialization.go b/stacks/arraystack/serialization.go index 6d26fa72..1456c9ec 100644 --- a/stacks/arraystack/serialization.go +++ b/stacks/arraystack/serialization.go @@ -5,16 +5,12 @@ package arraystack 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. @@ -27,12 +23,12 @@ func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (stack *Stack) UnmarshalJSON(bytes []byte) error { return stack.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (stack *Stack) MarshalJSON() ([]byte, error) { return stack.ToJSON() } diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index aeebc310..6b34e39c 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -5,6 +5,7 @@ package linkedliststack import ( + "encoding/json" "fmt" "strings" "testing" @@ -222,11 +223,16 @@ func TestStackSerialization(t *testing.T) { assert() - json, err := stack.ToJSON() + bytes, err := stack.ToJSON() assert() - err = stack.FromJSON(json) + err = stack.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", stack}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkPush(b *testing.B, stack *Stack, size int) { diff --git a/stacks/linkedliststack/serialization.go b/stacks/linkedliststack/serialization.go index fce1511e..3beb7a80 100644 --- a/stacks/linkedliststack/serialization.go +++ b/stacks/linkedliststack/serialization.go @@ -5,16 +5,12 @@ package linkedliststack 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. @@ -27,12 +23,12 @@ func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (stack *Stack) UnmarshalJSON(bytes []byte) error { return stack.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (stack *Stack) MarshalJSON() ([]byte, error) { return stack.ToJSON() } diff --git a/trees/avltree/avltree_test.go b/trees/avltree/avltree_test.go index 9cc300a4..37bdd2f6 100644 --- a/trees/avltree/avltree_test.go +++ b/trees/avltree/avltree_test.go @@ -4,6 +4,7 @@ package avltree import ( + "encoding/json" "fmt" "strings" "testing" @@ -731,11 +732,16 @@ func TestAVLTreeSerialization(t *testing.T) { assert() - json, err := tree.ToJSON() + bytes, err := tree.ToJSON() assert() - err = tree.FromJSON(json) + err = tree.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, tree *Tree, size int) { diff --git a/trees/avltree/serialization.go b/trees/avltree/serialization.go index 79d4e5e9..1ccd4a81 100644 --- a/trees/avltree/serialization.go +++ b/trees/avltree/serialization.go @@ -13,8 +13,6 @@ 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. @@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (tree *Tree) UnmarshalJSON(bytes []byte) error { return tree.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (tree *Tree) MarshalJSON() ([]byte, error) { return tree.ToJSON() } diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index 7a3d0605..e14794f1 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -5,6 +5,7 @@ package binaryheap import ( + "encoding/json" "math/rand" "strings" "testing" @@ -391,11 +392,16 @@ func TestBinaryHeapSerialization(t *testing.T) { assert() - json, err := heap.ToJSON() + bytes, err := heap.ToJSON() assert() - err = heap.FromJSON(json) + err = heap.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", heap}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkPush(b *testing.B, heap *Heap, size int) { diff --git a/trees/binaryheap/serialization.go b/trees/binaryheap/serialization.go index e0d2a939..30ccb250 100644 --- a/trees/binaryheap/serialization.go +++ b/trees/binaryheap/serialization.go @@ -5,16 +5,12 @@ package binaryheap 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. @@ -27,12 +23,12 @@ func (heap *Heap) FromJSON(data []byte) error { return heap.list.FromJSON(data) } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (heap *Heap) UnmarshalJSON(bytes []byte) error { return heap.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (heap *Heap) MarshalJSON() ([]byte, error) { return heap.ToJSON() } diff --git a/trees/btree/btree_test.go b/trees/btree/btree_test.go index 5bf5cb4d..49c6ab5d 100644 --- a/trees/btree/btree_test.go +++ b/trees/btree/btree_test.go @@ -5,6 +5,7 @@ package btree import ( + "encoding/json" "fmt" "strings" "testing" @@ -1251,11 +1252,16 @@ func TestBTreeSerialization(t *testing.T) { assert() - json, err := tree.ToJSON() + bytes, err := tree.ToJSON() assert() - err = tree.FromJSON(json) + err = tree.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, tree *Tree, size int) { diff --git a/trees/btree/serialization.go b/trees/btree/serialization.go index 75ef7280..5ff71751 100644 --- a/trees/btree/serialization.go +++ b/trees/btree/serialization.go @@ -13,8 +13,6 @@ 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. @@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (tree *Tree) UnmarshalJSON(bytes []byte) error { return tree.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (tree *Tree) MarshalJSON() ([]byte, error) { return tree.ToJSON() } diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index 3abe6c1a..c717648c 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -5,6 +5,7 @@ package redblacktree import ( + "encoding/json" "fmt" "strings" "testing" @@ -734,11 +735,16 @@ func TestRedBlackTreeSerialization(t *testing.T) { assert() - json, err := tree.ToJSON() + bytes, err := tree.ToJSON() assert() - err = tree.FromJSON(json) + err = tree.FromJSON(bytes) assert() + + bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree}) + if err != nil { + t.Errorf("Got error %v", err) + } } func benchmarkGet(b *testing.B, tree *Tree, size int) { diff --git a/trees/redblacktree/serialization.go b/trees/redblacktree/serialization.go index 84c48216..da137771 100644 --- a/trees/redblacktree/serialization.go +++ b/trees/redblacktree/serialization.go @@ -13,8 +13,6 @@ 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. @@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error { return err } -// @implements json.Unmarshaler +// UnmarshalJSON @implements json.Unmarshaler func (tree *Tree) UnmarshalJSON(bytes []byte) error { return tree.FromJSON(bytes) } -// @implements json.Marshaler +// MarshalJSON @implements json.Marshaler func (tree *Tree) MarshalJSON() ([]byte, error) { return tree.ToJSON() }