From 232f8d8a62b386dd2a8a51880b7162f31b84ee23 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Mon, 6 Mar 2017 04:35:57 +0100 Subject: [PATCH] - tree-map and tree-bidi-map (de)serialization --- maps/treebidimap/serialization.go | 39 ++++++++++++++++++++++++++++ maps/treebidimap/treebidimap_test.go | 31 ++++++++++++++++++++++ maps/treemap/serialization.go | 22 ++++++++++++++++ maps/treemap/treemap_test.go | 31 ++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 maps/treebidimap/serialization.go create mode 100644 maps/treemap/serialization.go diff --git a/maps/treebidimap/serialization.go b/maps/treebidimap/serialization.go new file mode 100644 index 0000000..f9a7850 --- /dev/null +++ b/maps/treebidimap/serialization.go @@ -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 +} diff --git a/maps/treebidimap/treebidimap_test.go b/maps/treebidimap/treebidimap_test.go index c504e97..0ef5165 100644 --- a/maps/treebidimap/treebidimap_test.go +++ b/maps/treebidimap/treebidimap_test.go @@ -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++ { diff --git a/maps/treemap/serialization.go b/maps/treemap/serialization.go new file mode 100644 index 0000000..25f4a6f --- /dev/null +++ b/maps/treemap/serialization.go @@ -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) +} diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 0039601..a73e873 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -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++ {