Skip to content

Commit d677e40

Browse files
committed
Add flattenNestedMapWithKeys method for stripe conversion to application/x-www-form-urlencoded
1 parent f6e7025 commit d677e40

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

framework/src/main/java/org/moqui/util/CollectionUtilities.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,39 @@ public static Map flattenNestedMap(Map theMap) {
460460
return outMap;
461461
}
462462

463+
public static Map<String, String> flattenNestedMapWithKeys(Map<String, Object> theMap) {
464+
return flattenNestedMapWithKeys(theMap, "");
465+
}
466+
467+
@SuppressWarnings("unchecked")
468+
private static Map<String, String> flattenNestedMapWithKeys(Map<String, Object> theMap, String parentKey) {
469+
Map<String, String> output = new LinkedHashMap<>();
470+
471+
if (theMap == null) return output;
472+
473+
for (Map.Entry<String, Object> entry : theMap.entrySet()) {
474+
String key = entry.getKey();
475+
Object value = entry.getValue();
476+
String newKey = parentKey.isEmpty() ? key : parentKey + "[" + key + "]";
477+
478+
if (value instanceof Map) {
479+
output.putAll(flattenNestedMapWithKeys((Map<String, Object>) value, newKey));
480+
} else if (value instanceof Collection) {
481+
int index = 0;
482+
for (Object colValue : (Collection<?>) value) {
483+
if (colValue instanceof Map) {
484+
output.putAll(flattenNestedMapWithKeys((Map<String, Object>) colValue, newKey + "[" + index + "]"));
485+
} else {
486+
output.put(newKey + "[" + index + "]", colValue.toString());
487+
}
488+
index++;
489+
}
490+
} else {
491+
output.put(newKey, value.toString());
492+
}
493+
}
494+
return output;
495+
}
463496
@SuppressWarnings("unchecked")
464497
public static void mergeNestedMap(Map<Object, Object> baseMap, Map<Object, Object> overrideMap, boolean overrideEmpty) {
465498
if (baseMap == null || overrideMap == null) return;

0 commit comments

Comments
 (0)