diff --git a/util.go b/util.go index fa8ed1d..b8eb9fb 100644 --- a/util.go +++ b/util.go @@ -11,7 +11,17 @@ func StringifyKeys(m map[interface{}]interface{}) (out map[string]interface{}) { out = map[string]interface{}{} for k, v := range m { - if x, ok := v.(map[interface{}]interface{}); ok { + switch x := v.(type) { + case []interface{}: + newSlice := []interface{}{} + for _, sliceEntry := range x { + if subMap, ok := sliceEntry.(map[interface{}]interface{}); ok { + sliceEntry = StringifyKeys(subMap) + } + newSlice = append(newSlice, sliceEntry) + } + v = newSlice + case map[interface{}]interface{}: v = StringifyKeys(x) } diff --git a/util_test.go b/util_test.go index 442188b..a9f9e5e 100644 --- a/util_test.go +++ b/util_test.go @@ -53,3 +53,31 @@ func TestStringifyKeysOnMapWithEntriesRecursively(t *testing.T) { t.Errorf("Expected:\n %#+v\nGot:\n %#+v", expected, result) } } + +func TestStringifyKeysOnMapRecursivelyMovesThroughSlices(t *testing.T) { + m := map[interface{}]interface{}{ + "foo": []interface{}{ + map[interface{}]interface{}{ + "foo": "bar", + "baz": 123, + }, + 123, + }, + "baz": 123, + } + result := phpserialize.StringifyKeys(m) + expected := map[string]interface{}{ + "foo": []interface{}{ + map[string]interface{}{ + "foo": "bar", + "baz": 123, + }, + 123, + }, + "baz": 123, + } + + if !reflect.DeepEqual(result, expected) { + t.Errorf("Expected:\n %#+v\nGot:\n %#+v", expected, result) + } +}