@ SuppressWarnings ( "unchecked" ) private Map < String , Object > convertToMap ( Object yamlDocument ) { Map < String , Object > yamlMap = new LinkedHashMap <> ( ) ; // Document is a text block if ( ! ( yamlDocument instanceof Map ) ) { yamlMap . put ( "content" , yamlDocument ) ; return yamlMap ; } for ( Map . Entry < Object , Object > entry : ( ( Map < Object , Object > ) yamlDocument ) . entrySet ( ) ) { Object value = entry . getValue ( ) ; if ( value instanceof Map ) { value = convertToMap ( value ) ; } else if ( value instanceof Collection ) { ArrayList < Map < String , Object > > collection = new ArrayList <> ( ) ; for ( Object element : ( ( Collection ) value ) ) { collection . add ( convertToMap ( element ) ) ; } value = collection ; } yamlMap . put ( entry . getKey ( ) . toString ( ) , value ) ; } return yamlMap ; }
Convert given Yaml document to a multi - level map .