diff --git a/doc.go b/doc.go index cf92a06..d9d3979 100644 --- a/doc.go +++ b/doc.go @@ -14,6 +14,7 @@ Related Packages: checkxml: github.com/clbanning/checkxml provides functions for validating XML data. Notes: + 2018.03.29: mv.Gob/NewMapGob support gob encoding/decoding of Maps. 2018.03.26: Added mxj/x2j-wrapper sub-package for migrating from legacy x2j package. 2017.02.22: LeafNode paths can use ".N" syntax rather than "[N]" for list member indexing. 2017.02.21: github.com/clbanning/checkxml provides functions for validating XML data. diff --git a/gob.go b/gob.go new file mode 100644 index 0000000..d56c2fd --- /dev/null +++ b/gob.go @@ -0,0 +1,35 @@ +// gob.go - Encode/Decode a Map into a gob object. + +package mxj + +import ( + "bytes" + "encoding/gob" +) + +// NewMapGob returns a Map value for a gob object that has been +// encoded from a map[string]interface{} (or compatible type) value. +// It is intended to provide symmetric handling of Maps that have +// been encoded using mv.Gob. +func NewMapGob(gobj []byte) (Map, error) { + m := make(map[string]interface{}, 0) + if len(gobj) == 0 { + return m, nil + } + r := bytes.NewReader(gobj) + dec := gob.NewDecoder(r) + if err := dec.Decode(&m); err != nil { + return m, err + } + return m, nil +} + +// Gob returns a gob-encoded value for the Map 'mv'. +func (mv Map) Gob() ([]byte, error) { + var buf bytes.Buffer + enc := gob.NewEncoder(&buf) + if err := enc.Encode(map[string]interface{}(mv)); err != nil { + return nil, err + } + return buf.Bytes(), nil +} diff --git a/gob_test.go b/gob_test.go new file mode 100644 index 0000000..6180baa --- /dev/null +++ b/gob_test.go @@ -0,0 +1,50 @@ +package mxj + +import ( + "bytes" + "encoding/gob" + "fmt" + "testing" +) + +var gobData = map[string]interface{}{ + "one": 1, + "two": 2.0001, + "three": "tres", + "four": []int{1, 2, 3, 4}, + "five": map[string]interface{}{"hi": "there"}} + +func TestGobHeader(t *testing.T) { + fmt.Println("\n---------------- gob_test.go ...") +} + +func TestNewMapGob(t *testing.T) { + var buf bytes.Buffer + gob.Register(gobData) + enc := gob.NewEncoder(&buf) + if err := enc.Encode(gobData); err != nil { + t.Fatal("enc.Encode err:", err.Error()) + } + // decode 'buf' into a Map - map[string]interface{} + m, err := NewMapGob(buf.Bytes()) + if err != nil { + t.Fatal("NewMapGob err:", err.Error()) + } + fmt.Printf("m: %v\n", m) +} + +func TestMapGob(t *testing.T) { + mv := Map(gobData) + g, err := mv.Gob() + if err != nil { + t.Fatal("m.Gob err:", err.Error()) + } + // decode 'g' into a map[string]interface{} + m := make(map[string]interface{}) + r := bytes.NewReader(g) + dec := gob.NewDecoder(r) + if err := dec.Decode(&m); err != nil { + t.Fatal("dec.Decode err:", err.Error()) + } + fmt.Printf("m: %v\n", m) +} diff --git a/readme.md b/readme.md index f5a99c4..5c7f036 100644 --- a/readme.md +++ b/readme.md @@ -21,6 +21,7 @@ For over a year I've wanted to refactor the XML-to-map[string]interface{} decode

Notices

+ 2018.03.29: mv.Gob/NewMapGob support gob encoding/decoding of Maps. 2018.03.26: Added mxj/x2j-wrapper sub-package for migrating from legacy x2j package. 2017.02.22: LeafNode paths can use ".N" syntax rather than "[N]" for list member indexing. 2017.02.10: SetFieldSeparator changes field separator for args in UpdateValuesForPath, ValuesFor... methods.