Open Nicklas2751 opened 7 years ago
Hi @Nicklas2751, first things first, thanks a lot for your feedback.
I'll try to elaborate a little bit more your use case just to make sure that my understanding is correct (or wrong).
From your comment, it seems that you are reading some YAML configuration and, I guess, you are mapping that configuration to some Java POJO. What you would like to do (and I'm really speculating here) is to change some property of the POJO (maybe through some setter) and then have the same changes reflected in the original YAML file used as source.
Is that your use case?
Yes it is.
My personal feeling (and I may be wrong here) is that this feature is a bit outside the scope of a configuration library, as far as the common interpretation of "configuration library" goes.
I've just double checked other configuration libraries (to make sure I didn't miss anything obvious like this) and, unfortunately, none of them seem to cover this particular use case either.
Now, getting back to your scenario, would it be too bad to handle the write part using something like Jackson? You would need to just create a YAML-specific ObjectMapper (something similar to what I do here) and use the writeValue method.
I tried it with using your ObjectMapper and writing with Jackson back to YAML but the YAML it generated had other information las the original.
In the original i have something like this:
logLevelFile: ERROR
which Type is a org.apache.logging.log4j.Level
and if i generate and write it to a file Jackson makes something like this:
logLevelFile:
standardLevel: "INFO"
declaringClass: "org.apache.logging.log4j.Level"
The reasons why you are getting that is due to how Jackson serialization/deserialization works and the fact that org.apache.logging.log4j.Level
is out of your control (i.e. you can't change the class so that it gets serialized as you would like).
When deserializing (i.e. reading the configuration through yacl4j), Jackson automatically detects that there is a static Level#valueOf
method and uses it to obtain an instance of Level from the string you write in the YAML file.
When serializing (i.e. writing the configuration with your custom code), Jackson inspects the Level class and finds that there are two getters (getStandardLevel
and getDeclaringClass
) and uses them to create the YAML representation of the Level object.
If you keep using Jackson, I guess you have (at least) two options:
org.apache.logging.log4j.Level
class with a simple String
in the POJO that represents your YAML configuration;org.apache.logging.log4j.Level
.I hope that helped.
I found this project when i was searching for a library to handle YAML configurations for mediathekview/MServer. The configuration reading works really fine and I'm very glad with it. But I'm very sad about the fact, there is no option to save a changed configuration back to a YAML file.
Please add support for writing the configuration back to YAML.