Implements json.Marshaler and json.Unmarshaler interfaces

pull/192/head
Emir Pasic 2 years ago
parent b5735bcc4d
commit 1f0b87f0e1

@ -8,10 +8,14 @@ package containers
type JSONSerializer interface { type JSONSerializer interface {
// ToJSON outputs the JSON representation of containers's elements. // ToJSON outputs the JSON representation of containers's elements.
ToJSON() ([]byte, error) ToJSON() ([]byte, error)
// MarshalJSON @implements json.Marshaler
MarshalJSON() ([]byte, error)
} }
// JSONDeserializer provides JSON deserialization // JSONDeserializer provides JSON deserialization
type JSONDeserializer interface { type JSONDeserializer interface {
// FromJSON populates containers's elements from the input JSON representation. // FromJSON populates containers's elements from the input JSON representation.
FromJSON([]byte) error FromJSON([]byte) error
// UnmarshalJSON @implements json.Unmarshaler
UnmarshalJSON([]byte) error
} }

@ -5,6 +5,7 @@
package arraylist package arraylist
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
"strings" "strings"
@ -620,11 +621,16 @@ func TestListSerialization(t *testing.T) {
assert() assert()
json, err := list.ToJSON() bytes, err := list.ToJSON()
assert() assert()
err = list.FromJSON(json) err = list.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, list *List, size int) {

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONSerializer = (*List)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of list's elements.
@ -30,12 +28,12 @@ func (list *List) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (list *List) UnmarshalJSON(bytes []byte) error { func (list *List) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes) return list.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (list *List) MarshalJSON() ([]byte, error) { func (list *List) MarshalJSON() ([]byte, error) {
return list.ToJSON() return list.ToJSON()
} }

@ -5,6 +5,7 @@
package doublylinkedlist package doublylinkedlist
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -626,11 +627,16 @@ func TestListSerialization(t *testing.T) {
assert() assert()
json, err := list.ToJSON() bytes, err := list.ToJSON()
assert() assert()
err = list.FromJSON(json) err = list.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, list *List, size int) {

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONSerializer = (*List)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of list's elements.
@ -32,12 +30,12 @@ func (list *List) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (list *List) UnmarshalJSON(bytes []byte) error { func (list *List) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes) return list.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (list *List) MarshalJSON() ([]byte, error) { func (list *List) MarshalJSON() ([]byte, error) {
return list.ToJSON() return list.ToJSON()
} }

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*List)(nil) var _ containers.JSONSerializer = (*List)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of list's elements.
@ -32,12 +30,12 @@ func (list *List) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (list *List) UnmarshalJSON(bytes []byte) error { func (list *List) UnmarshalJSON(bytes []byte) error {
return list.FromJSON(bytes) return list.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (list *List) MarshalJSON() ([]byte, error) { func (list *List) MarshalJSON() ([]byte, error) {
return list.ToJSON() return list.ToJSON()
} }

@ -5,6 +5,7 @@
package singlylinkedlist package singlylinkedlist
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -489,11 +490,16 @@ func TestListSerialization(t *testing.T) {
assert() assert()
json, err := list.ToJSON() bytes, err := list.ToJSON()
assert() assert()
err = list.FromJSON(json) err = list.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, list *List, size int) {

@ -5,6 +5,7 @@
package hashbidimap package hashbidimap
import ( import (
"encoding/json"
"fmt" "fmt"
"testing" "testing"
) )
@ -174,11 +175,16 @@ func TestMapSerialization(t *testing.T) {
assert() assert()
json, err := m.ToJSON() bytes, err := m.ToJSON()
assert() assert()
err = m.FromJSON(json) err = m.FromJSON(bytes)
assert() 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 { func sameElements(a []interface{}, b []interface{}) bool {

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the map.
@ -34,12 +32,12 @@ func (m *Map) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (m *Map) UnmarshalJSON(bytes []byte) error { func (m *Map) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes) return m.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (m *Map) MarshalJSON() ([]byte, error) { func (m *Map) MarshalJSON() ([]byte, error) {
return m.ToJSON() return m.ToJSON()
} }

@ -5,6 +5,7 @@
package hashmap package hashmap
import ( import (
"encoding/json"
"fmt" "fmt"
"testing" "testing"
) )
@ -142,11 +143,16 @@ func TestMapSerialization(t *testing.T) {
assert() assert()
json, err := m.ToJSON() bytes, err := m.ToJSON()
assert() assert()
err = m.FromJSON(json) err = m.FromJSON(bytes)
assert() 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 { func sameElements(a []interface{}, b []interface{}) bool {

@ -13,8 +13,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the map.
@ -39,12 +37,12 @@ func (m *Map) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (m *Map) UnmarshalJSON(bytes []byte) error { func (m *Map) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes) return m.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (m *Map) MarshalJSON() ([]byte, error) { func (m *Map) MarshalJSON() ([]byte, error) {
return m.ToJSON() return m.ToJSON()
} }

@ -5,6 +5,7 @@
package linkedhashmap package linkedhashmap
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -571,6 +572,16 @@ func TestMapSerialization(t *testing.T) {
} }
assertSerialization(deserialized, "C", 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 //noinspection GoBoolExpressions

@ -14,8 +14,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*Map)(nil) var _ containers.JSONDeserializer = (*Map)(nil)
var _ json.Marshaler = (*Map)(nil)
var _ json.Unmarshaler = (*Map)(nil)
} }
// ToJSON outputs the JSON representation of map. // ToJSON outputs the JSON representation of map.
@ -104,12 +102,12 @@ func (m *Map) FromJSON(data []byte) error {
return nil return nil
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (m *Map) UnmarshalJSON(bytes []byte) error { func (m *Map) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes) return m.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (m *Map) MarshalJSON() ([]byte, error) { func (m *Map) MarshalJSON() ([]byte, error) {
return m.ToJSON() return m.ToJSON()
} }

@ -13,8 +13,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the map.
@ -40,12 +38,12 @@ func (m *Map) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (m *Map) UnmarshalJSON(bytes []byte) error { func (m *Map) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes) return m.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (m *Map) MarshalJSON() ([]byte, error) { func (m *Map) MarshalJSON() ([]byte, error) {
return m.ToJSON() return m.ToJSON()
} }

@ -5,6 +5,7 @@
package treebidimap package treebidimap
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
"strings" "strings"
@ -604,6 +605,16 @@ func TestMapSerialization(t *testing.T) {
} }
assertSerialization(deserialized, "C", 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 //noinspection GoBoolExpressions

@ -5,16 +5,12 @@
package treemap package treemap
import ( import (
"encoding/json"
"github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/containers"
) )
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil) var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the map.
@ -27,12 +23,12 @@ func (m *Map) FromJSON(data []byte) error {
return m.tree.FromJSON(data) return m.tree.FromJSON(data)
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (m *Map) UnmarshalJSON(bytes []byte) error { func (m *Map) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes) return m.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (m *Map) MarshalJSON() ([]byte, error) { func (m *Map) MarshalJSON() ([]byte, error) {
return m.ToJSON() return m.ToJSON()
} }

@ -5,6 +5,7 @@
package treemap package treemap
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -627,6 +628,16 @@ func TestMapSerialization(t *testing.T) {
} }
assertSerialization(deserialized, "C", 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 //noinspection GoBoolExpressions

@ -5,6 +5,7 @@
package hashset package hashset
import ( import (
"encoding/json"
"testing" "testing"
) )
@ -98,11 +99,16 @@ func TestSetSerialization(t *testing.T) {
assert() assert()
json, err := set.ToJSON() bytes, err := set.ToJSON()
assert() assert()
err = set.FromJSON(json) err = set.FromJSON(bytes)
assert() 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) { func benchmarkContains(b *testing.B, set *Set, size int) {

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONSerializer = (*Set)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the set.
@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (set *Set) UnmarshalJSON(bytes []byte) error { func (set *Set) UnmarshalJSON(bytes []byte) error {
return set.FromJSON(bytes) return set.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (set *Set) MarshalJSON() ([]byte, error) { func (set *Set) MarshalJSON() ([]byte, error) {
return set.ToJSON() return set.ToJSON()
} }

@ -5,6 +5,7 @@
package linkedhashset package linkedhashset
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -452,11 +453,16 @@ func TestSetSerialization(t *testing.T) {
assert() assert()
json, err := set.ToJSON() bytes, err := set.ToJSON()
assert() assert()
err = set.FromJSON(json) err = set.FromJSON(bytes)
assert() 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) { func benchmarkContains(b *testing.B, set *Set, size int) {

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONSerializer = (*Set)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the set.
@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (set *Set) UnmarshalJSON(bytes []byte) error { func (set *Set) UnmarshalJSON(bytes []byte) error {
return set.FromJSON(bytes) return set.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (set *Set) MarshalJSON() ([]byte, error) { func (set *Set) MarshalJSON() ([]byte, error) {
return set.ToJSON() return set.ToJSON()
} }

@ -12,8 +12,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Set)(nil) var _ containers.JSONSerializer = (*Set)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the set.
@ -32,12 +30,12 @@ func (set *Set) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (set *Set) UnmarshalJSON(bytes []byte) error { func (set *Set) UnmarshalJSON(bytes []byte) error {
return set.FromJSON(bytes) return set.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (set *Set) MarshalJSON() ([]byte, error) { func (set *Set) MarshalJSON() ([]byte, error) {
return set.ToJSON() return set.ToJSON()
} }

@ -5,6 +5,7 @@
package treeset package treeset
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -461,11 +462,16 @@ func TestSetSerialization(t *testing.T) {
assert() assert()
json, err := set.ToJSON() bytes, err := set.ToJSON()
assert() assert()
err = set.FromJSON(json) err = set.FromJSON(bytes)
assert() 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) { func benchmarkContains(b *testing.B, set *Set, size int) {

@ -5,6 +5,7 @@
package arraystack package arraystack
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -360,11 +361,16 @@ func TestStackSerialization(t *testing.T) {
assert() assert()
json, err := stack.ToJSON() bytes, err := stack.ToJSON()
assert() assert()
err = stack.FromJSON(json) err = stack.FromJSON(bytes)
assert() 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) { func benchmarkPush(b *testing.B, stack *Stack, size int) {

@ -5,16 +5,12 @@
package arraystack package arraystack
import ( import (
"encoding/json"
"github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/containers"
) )
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Stack)(nil) var _ containers.JSONSerializer = (*Stack)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the stack.
@ -27,12 +23,12 @@ func (stack *Stack) FromJSON(data []byte) error {
return stack.list.FromJSON(data) return stack.list.FromJSON(data)
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (stack *Stack) UnmarshalJSON(bytes []byte) error { func (stack *Stack) UnmarshalJSON(bytes []byte) error {
return stack.FromJSON(bytes) return stack.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (stack *Stack) MarshalJSON() ([]byte, error) { func (stack *Stack) MarshalJSON() ([]byte, error) {
return stack.ToJSON() return stack.ToJSON()
} }

@ -5,6 +5,7 @@
package linkedliststack package linkedliststack
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -222,11 +223,16 @@ func TestStackSerialization(t *testing.T) {
assert() assert()
json, err := stack.ToJSON() bytes, err := stack.ToJSON()
assert() assert()
err = stack.FromJSON(json) err = stack.FromJSON(bytes)
assert() 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) { func benchmarkPush(b *testing.B, stack *Stack, size int) {

@ -5,16 +5,12 @@
package linkedliststack package linkedliststack
import ( import (
"encoding/json"
"github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/containers"
) )
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Stack)(nil) var _ containers.JSONSerializer = (*Stack)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the stack.
@ -27,12 +23,12 @@ func (stack *Stack) FromJSON(data []byte) error {
return stack.list.FromJSON(data) return stack.list.FromJSON(data)
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (stack *Stack) UnmarshalJSON(bytes []byte) error { func (stack *Stack) UnmarshalJSON(bytes []byte) error {
return stack.FromJSON(bytes) return stack.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (stack *Stack) MarshalJSON() ([]byte, error) { func (stack *Stack) MarshalJSON() ([]byte, error) {
return stack.ToJSON() return stack.ToJSON()
} }

@ -4,6 +4,7 @@
package avltree package avltree
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -731,11 +732,16 @@ func TestAVLTreeSerialization(t *testing.T) {
assert() assert()
json, err := tree.ToJSON() bytes, err := tree.ToJSON()
assert() assert()
err = tree.FromJSON(json) err = tree.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, tree *Tree, size int) {

@ -13,8 +13,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONSerializer = (*Tree)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the tree.
@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (tree *Tree) UnmarshalJSON(bytes []byte) error { func (tree *Tree) UnmarshalJSON(bytes []byte) error {
return tree.FromJSON(bytes) return tree.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (tree *Tree) MarshalJSON() ([]byte, error) { func (tree *Tree) MarshalJSON() ([]byte, error) {
return tree.ToJSON() return tree.ToJSON()
} }

@ -5,6 +5,7 @@
package binaryheap package binaryheap
import ( import (
"encoding/json"
"math/rand" "math/rand"
"strings" "strings"
"testing" "testing"
@ -391,11 +392,16 @@ func TestBinaryHeapSerialization(t *testing.T) {
assert() assert()
json, err := heap.ToJSON() bytes, err := heap.ToJSON()
assert() assert()
err = heap.FromJSON(json) err = heap.FromJSON(bytes)
assert() 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) { func benchmarkPush(b *testing.B, heap *Heap, size int) {

@ -5,16 +5,12 @@
package binaryheap package binaryheap
import ( import (
"encoding/json"
"github.com/emirpasic/gods/containers" "github.com/emirpasic/gods/containers"
) )
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Heap)(nil) var _ containers.JSONSerializer = (*Heap)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the heap.
@ -27,12 +23,12 @@ func (heap *Heap) FromJSON(data []byte) error {
return heap.list.FromJSON(data) return heap.list.FromJSON(data)
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (heap *Heap) UnmarshalJSON(bytes []byte) error { func (heap *Heap) UnmarshalJSON(bytes []byte) error {
return heap.FromJSON(bytes) return heap.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (heap *Heap) MarshalJSON() ([]byte, error) { func (heap *Heap) MarshalJSON() ([]byte, error) {
return heap.ToJSON() return heap.ToJSON()
} }

@ -5,6 +5,7 @@
package btree package btree
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -1251,11 +1252,16 @@ func TestBTreeSerialization(t *testing.T) {
assert() assert()
json, err := tree.ToJSON() bytes, err := tree.ToJSON()
assert() assert()
err = tree.FromJSON(json) err = tree.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, tree *Tree, size int) {

@ -13,8 +13,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONSerializer = (*Tree)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the tree.
@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (tree *Tree) UnmarshalJSON(bytes []byte) error { func (tree *Tree) UnmarshalJSON(bytes []byte) error {
return tree.FromJSON(bytes) return tree.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (tree *Tree) MarshalJSON() ([]byte, error) { func (tree *Tree) MarshalJSON() ([]byte, error) {
return tree.ToJSON() return tree.ToJSON()
} }

@ -5,6 +5,7 @@
package redblacktree package redblacktree
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -734,11 +735,16 @@ func TestRedBlackTreeSerialization(t *testing.T) {
assert() assert()
json, err := tree.ToJSON() bytes, err := tree.ToJSON()
assert() assert()
err = tree.FromJSON(json) err = tree.FromJSON(bytes)
assert() 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) { func benchmarkGet(b *testing.B, tree *Tree, size int) {

@ -13,8 +13,6 @@ import (
func assertSerializationImplementation() { func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Tree)(nil) var _ containers.JSONSerializer = (*Tree)(nil)
var _ containers.JSONDeserializer = (*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. // ToJSON outputs the JSON representation of the tree.
@ -40,12 +38,12 @@ func (tree *Tree) FromJSON(data []byte) error {
return err return err
} }
// @implements json.Unmarshaler // UnmarshalJSON @implements json.Unmarshaler
func (tree *Tree) UnmarshalJSON(bytes []byte) error { func (tree *Tree) UnmarshalJSON(bytes []byte) error {
return tree.FromJSON(bytes) return tree.FromJSON(bytes)
} }
// @implements json.Marshaler // MarshalJSON @implements json.Marshaler
func (tree *Tree) MarshalJSON() ([]byte, error) { func (tree *Tree) MarshalJSON() ([]byte, error) {
return tree.ToJSON() return tree.ToJSON()
} }

Loading…
Cancel
Save