Serialization
All data structures can be serialized (marshalled) and deserialized (unmarshalled). Currently only JSON support is available.
JSONSerializer
Outputs the container into its JSON representation.
Typical usage for key-value structures:
package main
import (
"fmt"
"github.com/emirpasic/gods/maps/hashmap"
)
func main() {
m := hashmap.New()
m.Put("a", "1")
m.Put("b", "2")
m.Put("c", "3")
json, err := m.ToJSON()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // {"a":"1","b":"2","c":"3"}
Typical usage for value-only structures:
package main
import (
"fmt"
"github.com/emirpasic/gods/lists/arraylist"
)
func main() {
list := arraylist.New()
list.Add("a", "b", "c")
json, err := list.ToJSON()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(json)) // ["a","b","c"]
}
JSONDeserializer
Populates the container with elements from the input JSON representation.
Typical usage for key-value structures:
package main
import (
"fmt"
"github.com/emirpasic/gods/maps/hashmap"
)
func main() {
hm := hashmap.New()
json := []byte(`{"a":"1","b":"2"}`)
err := hm.FromJSON(json)
if err != nil {
fmt.Println(err)
}
fmt.Println(hm) // HashMap map[b:2 a:1]
}
Typical usage for value-only structures:
package main
import (
"fmt"
"github.com/emirpasic/gods/lists/arraylist"
)
func main() {
list := arraylist.New()
json := []byte(`["a","b"]`)
err := list.FromJSON(json)
if err != nil {
fmt.Println(err)
}
fmt.Println(list) // ArrayList ["a","b"]
}