fabriziocucci / yacl4j

Yet Another Configuration Library For Java
Apache License 2.0
19 stars 2 forks source link

Save config to file #5

Open Nicklas2751 opened 7 years ago

Nicklas2751 commented 7 years ago

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.

fabriziocucci commented 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?

Nicklas2751 commented 7 years ago

Yes it is.

fabriziocucci commented 7 years ago

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.

Nicklas2751 commented 7 years ago

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"
fabriziocucci commented 7 years ago

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:

I hope that helped.