@@ -296,6 +296,38 @@ private void handleProcessError(Resource resource, IOException ex) {
296
296
}
297
297
}
298
298
299
+ /**
300
+ * Customize how the {@code Object} keys in the raw Map from the YAML parser
301
+ * are turned into {@code String} keys in a {@code Map<String, Object>},
302
+ * as a preparation step to the flattening and conversion to a
303
+ * {@code Properties} instance.
304
+ * <p>The default implementation performs the following sanitization:
305
+ * <ul>
306
+ * <li>{@code String} keys escaped with brackets in the YAML have brackets
307
+ * doubled. These will be used as full keys, dot-separated, in the Properties.</li>
308
+ * <li>non-string keys (eg. integer keys) are escaped with single brackets.
309
+ * These will be used as indexes (no dot separator) in the Properties.</li>
310
+ * </ul>
311
+ *
312
+ * @param rawKey the raw key as parsed by the YAML parser
313
+ * @return the sanitized key in {@code String} form
314
+ */
315
+ protected String sanitizeKey (Object rawKey ) {
316
+ if (rawKey instanceof CharSequence csKey ) {
317
+ String key = csKey .toString ();
318
+ //detecting keys escaped in brackets, turning to double brackets in properties
319
+ if (csKey .length () > 0 && csKey .charAt (0 ) == '['
320
+ && csKey .charAt (csKey .length ()-1 ) == ']' ) {
321
+ key = key .replace ("[" , "[[" ).replace ("]" , "]]" );
322
+ }
323
+ return key ;
324
+ }
325
+ else {
326
+ // It has to be a map key in this case
327
+ return "[" + rawKey + "]" ;
328
+ }
329
+ }
330
+
299
331
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
300
332
private Map <String , Object > asMap (Object object ) {
301
333
// YAML can have numbers as keys
@@ -310,13 +342,8 @@ private Map<String, Object> asMap(Object object) {
310
342
if (value instanceof Map ) {
311
343
value = asMap (value );
312
344
}
313
- if (key instanceof CharSequence ) {
314
- result .put (key .toString (), value );
315
- }
316
- else {
317
- // It has to be a map key in this case
318
- result .put ("[" + key .toString () + "]" , value );
319
- }
345
+ String sanitizedKey = sanitizeKey (key );
346
+ result .put (sanitizedKey , value );
320
347
});
321
348
return result ;
322
349
}
@@ -379,7 +406,7 @@ protected final Map<String, Object> getFlattenedMap(Map<String, Object> source)
379
406
private void buildFlattenedMap (Map <String , Object > result , Map <String , Object > source , @ Nullable String path ) {
380
407
source .forEach ((key , value ) -> {
381
408
if (StringUtils .hasText (path )) {
382
- if (key .startsWith ("[" )) {
409
+ if (key .startsWith ("[" ) && ! key . startsWith ( "[[" ) ) {
383
410
key = path + key ;
384
411
}
385
412
else {
0 commit comments