I am trying to parse the following YAML content using Jackson in Kotlin.
template:
# More properties...
noise.max: 0.01
I get this exception:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "noise.max" ...
When I change my YAML to this, it works:
template:
# More properties...
noise:
max: 0.01
It seems like Jackson can't parse nested values if they are noted inline with dots as separators. Is this incorrect YAML, or unconventional?
I know that spring boot can parse this kind of nested YAML params, and I guess they use Jackson for it too. But I can't find a way how I can configure the ObjectMapper
so it works.
Can someone please help me an tell me how to configure ObjectMapper
or whatever else needs to be done?
In YAML, a dot is not a special character and simply part of the content. The first file contains two mappings, with the inner one having noise.max
as key, while the second file contains three mappings, where the innermost has max
as key and the one above that has noise
as key. These are different structures.
Spring boot maps YAML to Properties. It does so by concatenating nested keys via dots. If you do this, the result of both your YAML files will be:
template.noise.max = 0.01
And that is why it works with Spring boot.
Property files are a list of key/value pairs while YAML files describe a possibly complex node graph. Spring boot uses YAML as syntax sugar for Properties. If you use Jackson, you process the actual structure and not the simplified one that you get with Spring boot.
So the bottom line is: If you want to use a YAML library for loading YAML, you will not have this „feature“ of replacing nested maps with dots in keys. Theoretically you could use SnakeYAML to do some preprocessing at the event level to split such keys so that what you want is possible, but I wouldn't recommend it.