diff --git a/xml.go b/xml.go index 09c987f..28bf87b 100644 --- a/xml.go +++ b/xml.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "io" + "reflect" "sort" "strconv" "strings" @@ -811,6 +812,20 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp var elen int p := &pretty{pp.indent, pp.cnt, pp.padding, pp.mapDepth, pp.start} + // per issue #48, 18apr18 - try and coerce maps to map[string]interface{} + // Don't need for mapToXmlSeqIndent, since maps there are decoded by NewMapXmlSeq(). + if reflect.ValueOf(value).Kind() == reflect.Map { + if _, ok := value.(map[string]interface{}); !ok { + val := make(map[string]interface{}) + keys := reflect.ValueOf(value).MapKeys() + vv := reflect.ValueOf(value) + for _, k := range keys { + val[fmt.Sprint(k)] = vv.MapIndex(k).Interface() + } + value = val + } + } + switch value.(type) { // special handling of []interface{} values when len(value) == 0 case map[string]interface{}, []byte, string, float64, bool, int, int32, int64, float32, json.Number: