Solved Weirdness experienced with FileConfiguration.save()

Discussion in 'Plugin Development' started by Etsijä, May 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Etsijä

    I'll try to explain the problem with code excerpts, without the usual try...catch surrounding them (they are there of course in my code).

    To start with, I have this in my plugin's default config.yml:
    Code:
    csv_separator: ","
    Then the relevant code to handle the configs in my main class:
    Code:java
    1. File configFile;
    2. FileConfiguration config;
    3.  
    4. // Load the config
    5. configFile = new File(getDataFolder(), "config.yml");
    6. config = new YamlConfiguration();
    7. config.load(configFile);
    8.  
    9. final Map<String, Object> configParams = new HashMap<String, Object>();
    10. configParams.put("csv_separator", ",");
    11.  
    12. // Handle the defaults
    13. for (final Entry<String, Object> e : configParams.entrySet())
    14. if (!config.contains(e.getKey()))
    15. config.set(e.getKey(), e.getValue());
    16.  
    17. // Save the config
    18. config.save(configFile);


    After the first run, with an empty config file in my plugin folder, the config file changes into this:
    Code:
    csv_separator: ','
    so the double quotes change into single quotes.

    If I change the stuff inside the single quotes into
    Code:
    csv_separator: ';'
    then after the second run, the config.yml is
    Code:
    csv_separator: ;
    which is not valid YAML anymore! What gives? Do I need to escape stuff like , and ; to handle it correctly in config.yml?
     
  2. Etsijä Question: Did you change something in eclipse to get that?
     
  3. Offline

    Etsijä

    No, nothing's changed.
     
  4. Etsijä As far as I'm aware
    Code:
    csv_separator: ;
    is fine for YAML syntax.
     
  5. Offline

    Etsijä

    Ah, I think I begin to understand now. Somehow, it seems FileConfiguration's save()-method strips the "unneeded" quotation marks from config.yml. This is why my ";" gets stripped down to ; (which is still valid) but for example ',' stays as it is, since

    csv_separator: ,

    would not be valid YAML. So, nothing wrong with my code then? Just need to be careful for the sake of safeness to always recommend users of my plugin to put ';', ',' etc. to the plugin, since leaving them out could lead to invalid YAML (like the example above).

    What I'd like to still know is why does the save() method of FileConfiguration class strip the quotations where it deems fit to do so. I'm asking since I see a potential of misunderstanding the configfile's structure here. Example: a user of my plugin decides to use the ";" as a field separator in the CSV output (that would then be directly accepted by MS Excel). The plugin reduces the configfile into

    csv_separator: ;

    Later on, the user wants to change the field separator into a ",", since that is actually specified as THE field separator in the CSV specification. He thinks this would do it:

    csv_separator: ,

    but that would actually be invalid YAML. Is there then not a simple way to be able to retain the quotes around that string in my config.yml?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Etsijä It strips it down, because what's the point in an extra 2 characters that are unnecessary and just take up space? I understand your point about possible confusion but, as with most things, it's expected that the user at least knows valid and invalid syntax. They're free to run it through a yaml validator if they do not.
     
  7. Offline

    Etsijä

    Adam, true, that does make sense.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page