I report the bug present in Spring deserialize YAML as LinkedHashMap instead of ArrayList because I ran into the same problem.
I tried to update to SpringBoot 2.7.9 but issue persists.
My case is reading YAML file and assign value to Map<String, Object>
, but as in the eample present in StackOverflow, Spring map to LinkedHashMap instead of ArrayList.
I use Java 17.
Comment From: simonbasle
Spring Framework has limited integration for YAML, supporting SnakeYAML
as a deserialization library through YamlProcessor
, YamlPropertiesBeanFactory
and YamlMapBeanFactory
classes. However, there's no automated support for YAML. You have to manually configure and use the aforementioned classes.
Also, in order to support the historical primary use case of YAML from the Framework's perspective, these classes further process the deserialized YAML data structure. Most notably, when deserializing to a Properties
the processing seeks a flat representation of leaves in the YAML structure, with String values and flattened String keys. Dictionaries entries are turned into dot-separated "paths" and arrays entries are turned into [x]
indexed keys.
Note that the deserialized Map
available to YamlProcessor.MatchCallback#process(Properties, Map)
is the closest you can get to the SnakeYAML-returned structure, although some keys are sanitized / processed. That map is what gets exposed by YamlMapFactoryBean
btw.
Note also that SnakeYAML does appear to deserialize this kind of YAML into a List
of Maps
:
listOfMaps:
- name: first
position: 1
- name: second
position: 2
So you might want to consider one of the following alternatives:
- use YamlMapFactoryBean
to load the YAML content
- roll your own YamlProcessor
to load the YAML content
- roll your own deserialization using either SnakeYAML
directly, or another deserialization library