diff --git a/anyxml.go b/anyxml.go index 371aa62..e04c9d2 100644 --- a/anyxml.go +++ b/anyxml.go @@ -49,7 +49,7 @@ true -An example of encoding a map[interface{}]interface{} value with mixed key types is +An example of encoding a map[interface{}]interface{} value with mixed key types is in anyxml/examples/goofy_map.go. */ package anyxml @@ -57,13 +57,16 @@ package anyxml import ( "encoding/xml" "reflect" + + // "github.com/clbanning/mxj" + "github.com/clbanning/mxj/v2" ) // Default missingElementTag value. var missingElemTag = "element" // MissingElementTag is used to set the lable to be used -// for values that are not map[string]interface{} type. By default +// for values that are not map[string]interface{} type. By default // the tag label "element" is used. The default can be reset by // passing an empty string, "", argument: MissingElementTag(""). func MissingElementTag(s string) { @@ -127,6 +130,12 @@ func Xml(v interface{}, rootTag ...string) ([]byte, error) { b = []byte(*s) } + if xmlCheckIsValid { + if _, err = mxj.NewMapXml(b); err != nil { + return nil, err + } + } + return b, err } @@ -190,5 +199,11 @@ func XmlIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte, b = []byte(*s) } + if xmlCheckIsValid { + if _, err = mxj.NewMapXml(b); err != nil { + return nil, err + } + } + return b, err } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..49cd8f3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/clbanning/anymxj/v2 + +go 1.15 diff --git a/isvalid_test.go b/isvalid_test.go new file mode 100644 index 0000000..ece55b5 --- /dev/null +++ b/isvalid_test.go @@ -0,0 +1,28 @@ +package anyxml + +import ( + "encoding/json" + "fmt" + "testing" +) + +func TestXmlCheckIsValid(t *testing.T) { + fmt.Println("================== TestXmlCheckIsValid") + + data := []byte(`{"":"empty", "$invalid":"hex$", "entities":"<>&", "nil": null}`) + m := make(map[string]interface{}) + err := json.Unmarshal(data, &m) + if err != nil { + t.Fatal("json.Unmarshal err;", err) + } + fmt.Printf("%v\n", m) + + XmlCheckIsValid() + if _, err := Xml(m); err == nil { + t.Fatal("Xml err: nil") + } + + if _, err := XmlIndent(m, "", " "); err == nil { + t.Fatal("Xml err: nil") + } +} diff --git a/readme.md b/readme.md index f416f3f..2c61453 100644 --- a/readme.md +++ b/readme.md @@ -17,6 +17,10 @@ See mxj package documentation for caveats, etc. Since some values, such as arrays, may require injecting tag labels to generate the XML, unmarshaling the resultant XML is not necessarily symmetric, i.e., you cannot get the original value back without some manipulation. +

Dependencies

+ + import github.com/clbanning/mxj/v2 +

Documentation

http://godoc.org/github.com/clbanning/anyxml diff --git a/xml.go b/xml.go index a5b2c33..7be0574 100644 --- a/xml.go +++ b/xml.go @@ -159,6 +159,20 @@ func escapeChars(s string) string { return string(b) } +// From clbanning/mxj issue #88 +// xmlCheckIsValid set switch to force decoding the encoded XML to +// see if it is valid XML. +var xmlCheckIsValid bool + +// XmlCheckIsValid forces the encoded XML to be checked for validity. +func XmlCheckIsValid(b ...bool) { + if len(b) == 1 { + xmlCheckIsValid = b[0] + return + } + xmlCheckIsValid = !xmlCheckIsValid +} + // where the work actually happens // returns an error if an attribute is not atomic // patched with new version in github.com/clbanning/mxj - 2015.11.15 @@ -199,7 +213,7 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp var n int var ss string for k, v := range vv { - if k[:1] == "-" { + if len(k) > 0 && k[:1] == "-" { switch v.(type) { case string: ss = v.(string) @@ -267,7 +281,7 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp elemlist := make([][2]interface{}, len(vv)) n = 0 for k, v := range vv { - if k[:1] == "-" { + if len(k) > 0 && k[:1] == "-" { continue } elemlist[n][0] = k