diff --git a/framework/src/main/java/org/moqui/util/CollectionUtilities.java b/framework/src/main/java/org/moqui/util/CollectionUtilities.java index 3252956a8..ea7fe9459 100644 --- a/framework/src/main/java/org/moqui/util/CollectionUtilities.java +++ b/framework/src/main/java/org/moqui/util/CollectionUtilities.java @@ -460,6 +460,39 @@ public static Map flattenNestedMap(Map theMap) { return outMap; } + public static Map flattenNestedMapWithKeys(Map theMap) { + return flattenNestedMapWithKeys(theMap, ""); + } + + @SuppressWarnings("unchecked") + private static Map flattenNestedMapWithKeys(Map theMap, String parentKey) { + Map output = new LinkedHashMap<>(); + + if (theMap == null) return output; + + for (Map.Entry entry : theMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + String newKey = parentKey.isEmpty() ? key : parentKey + "[" + key + "]"; + + if (value instanceof Map) { + output.putAll(flattenNestedMapWithKeys((Map) value, newKey)); + } else if (value instanceof Collection) { + int index = 0; + for (Object colValue : (Collection) value) { + if (colValue instanceof Map) { + output.putAll(flattenNestedMapWithKeys((Map) colValue, newKey + "[" + index + "]")); + } else { + output.put(newKey + "[" + index + "]", colValue.toString()); + } + index++; + } + } else { + output.put(newKey, value.toString()); + } + } + return output; + } @SuppressWarnings("unchecked") public static void mergeNestedMap(Map baseMap, Map overrideMap, boolean overrideEmpty) { if (baseMap == null || overrideMap == null) return;