Closed Elzorro99 closed 2 years ago
It is not possible at the moment because those strings do not need to be escaped, so to keep things simple and the most readable they are written in plain style without quotes.
What you describe would require a custom snakeyaml representer, and that is something you can't do with Simple-YAML at the moment. You would need to use snakeyaml directly.
If you mean to explicitly represent the words surrounded by "
character then you should do something like this:
yamlFile.set("double", "\"Hello world\"");
yamlFile.set("single", "'Hello world'");
These are the "Hello world"
and 'Hello world'
strings. They are represented in YAML using single quotes so they are not confused with the Hello world
string:
double: '"Hello world"'
single: '''Hello world'''
With single quote style the '
character needs to be repeated in order to escape it.
This library uses the PLAIN
ScalarStyle
, which is the snakeyaml default and the most readable style. It uses single quotes when escape is needed, as with my example above. Otherwise, it keeps the strings as is.
With snakeyaml you can change the default to use DumperOptions.ScalarStyle.DOUBLE_QUOTED
, in that case your output would be:
"language": "EN"
"string-list":
- "Hello"
- "World"
"update-notification": !!bool "true"
But this is less readable, so this library is not going to use it as the default. Probably it is not what you want anyway.
I would need this feature too! :)
I would need this feature too! :)
What is your use case? Loading files with strings using double quotes is perfectly valid if the user wants to write it like that, but saving the file will keep the strings as clean as possible while respecting the yaml specification. Double quotes are valid and sometimes useful for escaping some characters, but it's not the most readable if it is not needed, and for escaping characters currently single quotes are used.
In my case the file gets completely rewritten everytime my software starts. Most settings are copied from the old file to the new one, but the quotes get removed if no special characters are included in a value. That is a problem for values where special characters can occur because not everyone knows when to use quotes and when not.
i would like a option to do something like this:
yamlfile.set(path, object, quote.single/double)
This would help me and the users of the software a lot with some string where special characters or something like \n can occur.
@Cerexus
In my case the file gets completely rewritten everytime my software starts. Most settings are copied from the old file to the new one, but the quotes get removed if no special characters are included in a value. That is a problem for values where special characters can occur because not everyone knows when to use quotes and when not.
i would like a option to do something like this:
yamlfile.set(path, object, quote.single/double)
This would help me and the users of the software a lot with some string where special characters or something like \n can occur.
This feature has been included in the 1.8 release.
set(String path, Object value, QuoteStyle quoteStyle)
// By default strings are written without quotes if they are not needed so the configuration file remains clean
yamlFile.set("quotes.plain", "This is plain style");
// If a string contains special characters like # then it will be wrapped within single quotes
yamlFile.set("quotes.wrap", "# this is wrapped automatically with single quote style"); // this is a value, not a comment
// If you need it, you can enforce a quote style
yamlFile.set("quotes.custom", "This is double quote style", QuoteStyle.DOUBLE);
// You can change the quote style for all values with specific type
yamlFile.options().quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);
yamlFile.set("quotes.customDefault", "This is double quote style too");
quotes:
plain: This is plain style
wrap: '# this is wrapped automatically with single quote style'
custom: "This is double quote style"
customDefault: "This is double quote style too"
YamlExample
is updated with these changes.
@Elzorro99
Is it possible to save the values of type string and list of string between quotes ?
What I want :
This is how you do it after upgrading to 1.8 release:
// Create YamlFile with relative path
final YamlFile yamlFile = new YamlFile("examples/test-quote-style.yml");
// Load file
yamlFile.createOrLoadWithComments();
// (Optional) Change the side comment prefix with additional space
yamlFile.options().commentFormatter().formatterConfiguration(CommentType.SIDE).prefix(" # ");
// Change the quote style for String values
yamlFile.options().quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);
// Set language to EN with a side comment, using the alternative API (path method)
// This will use the quote style specified before
yamlFile.path("language").set("EN").commentSide("Only English(EN) or French(FR).");
// Change the quote style for List values
/*
List<String> is not a valid type at runtime, because of Java type erasure
If you need to have different quote styles for different types of lists then specify this line with
the desired quote style before setting the list values
*/
yamlFile.options().quoteStyleDefaults().setQuoteStyle(List.class, QuoteStyle.DOUBLE);
// Set the list values
// This will use the quote style specified before
yamlFile.set("string-list", Arrays.asList("Hello", "World"));
// Set another value
// This will use the default QuoteStyle.PLAIN, because Boolean.class QuoteStyle was not changed
yamlFile.set("update-notification", true);
// Save file
yamlFile.save();
language: "EN" # Only English(EN) or French(FR).
string-list:
- "Hello"
- "World"
update-notification: true
The snakeyaml default ScalarStyle can now also be changed using Simple-YAML:
yamlFile.options().quoteStyleDefaults().setDefaultQuoteStyle(QuoteStyle.DOUBLE);
This would change the default quote style for everything, keys and values of any type.
"language": "EN"
"string-list":
- "Hello"
- "World"
"update-notification": !!bool "true"
This default quote style can be overridden for any value with one of the ways stated in the previous comments.
Hello @Carleslc,
Is it possible to save the values of type string and list of string between quotes ?
That I have :
What I want :
Thank you for your help.