Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save config to file #5

Open
Nicklas2751 opened this issue Sep 1, 2017 · 5 comments
Open

Save config to file #5

Nicklas2751 opened this issue Sep 1, 2017 · 5 comments

Comments

@Nicklas2751
Copy link

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
Copy link
Owner

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
Copy link
Author

Yes it is.

@fabriziocucci
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

fabriziocucci commented Sep 1, 2017

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:

  • replace the org.apache.logging.log4j.Level class with a simple String in the POJO that represents your YAML configuration;
  • define a Jackson custom serializer for org.apache.logging.log4j.Level.

I hope that helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants