Skip to content

Commit

Permalink
Implements json.Marshaler and json.Unmarshaler interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
emirpasic committed Apr 12, 2022
1 parent b5735bc commit 1f0b87f
Show file tree
Hide file tree
Showing 35 changed files with 183 additions and 104 deletions.
4 changes: 4 additions & 0 deletions containers/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 8 additions & 2 deletions lists/arraylist/arraylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package arraylist

import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"strings"
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions lists/arraylist/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
10 changes: 8 additions & 2 deletions lists/doublylinkedlist/doublylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package doublylinkedlist

import (
"encoding/json"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions lists/doublylinkedlist/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
6 changes: 2 additions & 4 deletions lists/singlylinkedlist/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
10 changes: 8 additions & 2 deletions lists/singlylinkedlist/singlylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package singlylinkedlist

import (
"encoding/json"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions maps/hashbidimap/hashbidimap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package hashbidimap

import (
"encoding/json"
"fmt"
"testing"
)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions maps/hashbidimap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
10 changes: 8 additions & 2 deletions maps/hashmap/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package hashmap

import (
"encoding/json"
"fmt"
"testing"
)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions maps/hashmap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
11 changes: 11 additions & 0 deletions maps/linkedhashmap/linkedhashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package linkedhashmap

import (
"encoding/json"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions maps/linkedhashmap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
6 changes: 2 additions & 4 deletions maps/treebidimap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
11 changes: 11 additions & 0 deletions maps/treebidimap/treebidimap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package treebidimap

import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"strings"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions maps/treemap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
Loading

0 comments on commit 1f0b87f

Please sign in to comment.