mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-14 16:06:42 +03:00
Merge pull request #192 from emirpasic/json-interfaces
Implements json.Marshaler and json.Unmarshaler interfaces
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -27,3 +27,13 @@ func (list *List) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (list *List) UnmarshalJSON(bytes []byte) error {
|
||||
return list.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (list *List) MarshalJSON() ([]byte, error) {
|
||||
return list.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -29,3 +29,13 @@ func (list *List) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (list *List) UnmarshalJSON(bytes []byte) error {
|
||||
return list.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (list *List) MarshalJSON() ([]byte, error) {
|
||||
return list.ToJSON()
|
||||
}
|
||||
|
||||
@@ -29,3 +29,13 @@ func (list *List) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (list *List) UnmarshalJSON(bytes []byte) error {
|
||||
return list.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (list *List) MarshalJSON() ([]byte, error) {
|
||||
return list.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -31,3 +31,13 @@ func (m *Map) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (m *Map) UnmarshalJSON(bytes []byte) error {
|
||||
return m.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
return m.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -36,3 +36,13 @@ func (m *Map) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (m *Map) UnmarshalJSON(bytes []byte) error {
|
||||
return m.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
return m.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -101,3 +101,13 @@ func (m *Map) FromJSON(data []byte) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (m *Map) UnmarshalJSON(bytes []byte) error {
|
||||
return m.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
return m.ToJSON()
|
||||
}
|
||||
|
||||
@@ -37,3 +37,13 @@ func (m *Map) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (m *Map) UnmarshalJSON(bytes []byte) error {
|
||||
return m.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
return m.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
package treemap
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
import (
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Map)(nil)
|
||||
@@ -20,3 +22,13 @@ func (m *Map) ToJSON() ([]byte, error) {
|
||||
func (m *Map) FromJSON(data []byte) error {
|
||||
return m.tree.FromJSON(data)
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (m *Map) UnmarshalJSON(bytes []byte) error {
|
||||
return m.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
return m.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -29,3 +29,13 @@ func (set *Set) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (set *Set) UnmarshalJSON(bytes []byte) error {
|
||||
return set.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (set *Set) MarshalJSON() ([]byte, error) {
|
||||
return set.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -29,3 +29,13 @@ func (set *Set) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (set *Set) UnmarshalJSON(bytes []byte) error {
|
||||
return set.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (set *Set) MarshalJSON() ([]byte, error) {
|
||||
return set.ToJSON()
|
||||
}
|
||||
|
||||
@@ -29,3 +29,13 @@ func (set *Set) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (set *Set) UnmarshalJSON(bytes []byte) error {
|
||||
return set.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (set *Set) MarshalJSON() ([]byte, error) {
|
||||
return set.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
package arraystack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
import (
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
@@ -20,3 +22,13 @@ func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (stack *Stack) UnmarshalJSON(bytes []byte) error {
|
||||
return stack.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (stack *Stack) MarshalJSON() ([]byte, error) {
|
||||
return stack.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
package linkedliststack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
import (
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
@@ -20,3 +22,13 @@ func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (stack *Stack) UnmarshalJSON(bytes []byte) error {
|
||||
return stack.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (stack *Stack) MarshalJSON() ([]byte, error) {
|
||||
return stack.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -37,3 +37,13 @@ func (tree *Tree) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (tree *Tree) UnmarshalJSON(bytes []byte) error {
|
||||
return tree.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (tree *Tree) MarshalJSON() ([]byte, error) {
|
||||
return tree.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
package binaryheap
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
import (
|
||||
"github.com/emirpasic/gods/containers"
|
||||
)
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Heap)(nil)
|
||||
@@ -20,3 +22,13 @@ func (heap *Heap) ToJSON() ([]byte, error) {
|
||||
func (heap *Heap) FromJSON(data []byte) error {
|
||||
return heap.list.FromJSON(data)
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (heap *Heap) UnmarshalJSON(bytes []byte) error {
|
||||
return heap.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (heap *Heap) MarshalJSON() ([]byte, error) {
|
||||
return heap.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -37,3 +37,13 @@ func (tree *Tree) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (tree *Tree) UnmarshalJSON(bytes []byte) error {
|
||||
return tree.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (tree *Tree) MarshalJSON() ([]byte, error) {
|
||||
return tree.ToJSON()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -37,3 +37,13 @@ func (tree *Tree) FromJSON(data []byte) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalJSON @implements json.Unmarshaler
|
||||
func (tree *Tree) UnmarshalJSON(bytes []byte) error {
|
||||
return tree.FromJSON(bytes)
|
||||
}
|
||||
|
||||
// MarshalJSON @implements json.Marshaler
|
||||
func (tree *Tree) MarshalJSON() ([]byte, error) {
|
||||
return tree.ToJSON()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user