diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index 48f6be4..62ba7fd 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -5,6 +5,7 @@ package arraystack import ( + "fmt" "testing" ) @@ -231,6 +232,34 @@ func TestStackIteratorLast(t *testing.T) { } } +func TestStackSerialization(t *testing.T) { + stack := New() + stack.Push("a") + stack.Push("b") + stack.Push("c") + + var err error + assert := func() { + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if err != nil { + t.Errorf("Got error %v", err) + } + } + + assert() + + json, err := stack.ToJSON() + assert() + + err = stack.FromJSON(json) + assert() +} + func benchmarkPush(b *testing.B, stack *Stack, size int) { for i := 0; i < b.N; i++ { for n := 0; n < size; n++ { diff --git a/stacks/arraystack/serialization.go b/stacks/arraystack/serialization.go new file mode 100644 index 0000000..d1ad81d --- /dev/null +++ b/stacks/arraystack/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 arraystack + +import "github.com/emirpasic/gods/containers" + +func assertSerializationImplementation() { + var _ containers.JSONSerializer = (*Stack)(nil) + var _ containers.JSONDeserializer = (*Stack)(nil) +} + +// ToJSON outputs the JSON representation of list's elements. +func (stack *Stack) ToJSON() ([]byte, error) { + return stack.list.ToJSON() +} + +// FromJSON populates list's elements from the input JSON representation. +func (stack *Stack) FromJSON(data []byte) error { + return stack.list.FromJSON(data) +} diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index 8fb0f43..f7b6f95 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -5,6 +5,7 @@ package linkedliststack import ( + "fmt" "testing" ) @@ -147,6 +148,34 @@ func TestStackIteratorFirst(t *testing.T) { } } +func TestStackSerialization(t *testing.T) { + stack := New() + stack.Push("a") + stack.Push("b") + stack.Push("c") + + var err error + assert := func() { + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if err != nil { + t.Errorf("Got error %v", err) + } + } + + assert() + + json, err := stack.ToJSON() + assert() + + err = stack.FromJSON(json) + assert() +} + func benchmarkPush(b *testing.B, stack *Stack, size int) { for i := 0; i < b.N; i++ { for n := 0; n < size; n++ { diff --git a/stacks/linkedliststack/serialization.go b/stacks/linkedliststack/serialization.go new file mode 100644 index 0000000..ac6a68c --- /dev/null +++ b/stacks/linkedliststack/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 linkedliststack + +import "github.com/emirpasic/gods/containers" + +func assertSerializationImplementation() { + var _ containers.JSONSerializer = (*Stack)(nil) + var _ containers.JSONDeserializer = (*Stack)(nil) +} + +// ToJSON outputs the JSON representation of list's elements. +func (stack *Stack) ToJSON() ([]byte, error) { + return stack.list.ToJSON() +} + +// FromJSON populates list's elements from the input JSON representation. +func (stack *Stack) FromJSON(data []byte) error { + return stack.list.FromJSON(data) +}