- tree-map and tree-bidi-map (de)serialization

pull/53/head
Emir Pasic 7 years ago
parent 7eadb02f45
commit 232f8d8a62

@ -0,0 +1,39 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package treebidimap
import (
"encoding/json"
"github.com/emirpasic/gods/containers"
"github.com/emirpasic/gods/utils"
)
func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*Map)(nil)
}
// ToJSON outputs the JSON representation of list's elements.
func (m *Map) ToJSON() ([]byte, error) {
elements := make(map[string]interface{})
it := m.Iterator()
for it.Next() {
elements[utils.ToString(it.Key())] = it.Value()
}
return json.Marshal(&elements)
}
// FromJSON populates list's elements from the input JSON representation.
func (m *Map) FromJSON(data []byte) error {
elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements)
if err == nil {
m.Clear()
for key, value := range elements {
m.Put(key, value)
}
}
return err
}

@ -473,6 +473,37 @@ func TestMapIteratorLast(t *testing.T) {
}
}
func TestMapSerialization(t *testing.T) {
m := NewWithStringComparators()
m.Put("a", "1")
m.Put("b", "2")
m.Put("c", "3")
var err error
assert := func() {
if actualValue := m.Keys(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" {
t.Errorf("Got %v expected %v", actualValue, "[a,b,c]")
}
if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" {
t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
}
if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if err != nil {
t.Errorf("Got error %v", err)
}
}
assert()
json, err := m.ToJSON()
assert()
err = m.FromJSON(json)
assert()
}
func benchmarkGet(b *testing.B, m *Map, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {

@ -0,0 +1,22 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package treemap
import "github.com/emirpasic/gods/containers"
func assertSerializationImplementation() {
var _ containers.JSONSerializer = (*Map)(nil)
var _ containers.JSONDeserializer = (*Map)(nil)
}
// ToJSON outputs the JSON representation of list's elements.
func (m *Map) ToJSON() ([]byte, error) {
return m.tree.ToJSON()
}
// FromJSON populates list's elements from the input JSON representation.
func (m *Map) FromJSON(data []byte) error {
return m.tree.FromJSON(data)
}

@ -440,6 +440,37 @@ func TestMapIteratorLast(t *testing.T) {
}
}
func TestMapSerialization(t *testing.T) {
m := NewWithStringComparator()
m.Put("a", "1")
m.Put("b", "2")
m.Put("c", "3")
var err error
assert := func() {
if actualValue := m.Keys(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" {
t.Errorf("Got %v expected %v", actualValue, "[a,b,c]")
}
if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" {
t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
}
if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if err != nil {
t.Errorf("Got error %v", err)
}
}
assert()
json, err := m.ToJSON()
assert()
err = m.FromJSON(json)
assert()
}
func benchmarkGet(b *testing.B, m *Map, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {

Loading…
Cancel
Save