Comments (3)
Thank you @Carleslc!
This works like a charm for my requirements:
yamlFile.addDefault("default", new QuoteValue<>("test", QuoteStyle.DOUBLE));
from simple-yaml.
If you want to set a single default value with a custom quote style you can wrap the value with a QuoteValue
like this:
yamlFile.addDefault("default", new QuoteValue<>("test", QuoteStyle.DOUBLE));
If you want to write all default strings with double quotes you can set your own configuration defaults with the setDefaults
method, for instance you can use a YamlConfiguration
instance for your defaults (instead of the default MemoryConfiguration
), so you can use the set
method for your defaults:
YamlConfiguration defaults = new YamlConfiguration();
defaults.options().quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);
defaults.set("default", "test"); // String quote style is now DOUBLE by default
defaults.set("plain", "This is plain quote style", QuoteStyle.PLAIN); // You can override the quote style for specific values
yamlFile.setDefaults(defaults);
Output:
default: "test"
plain: This is plain quote style
Of course you can also avoid setting the quoteStyleDefaults
and use the set syntax providing the QuoteStyle for each default:
YamlConfiguration defaults = new YamlConfiguration();
defaults.set("quotes.custom", "This is double quote style", QuoteStyle.DOUBLE);
yamlFile.setDefaults(defaults);
quotes:
custom: "This is double quote style"
from simple-yaml.
Note that quote styles of default values will only be applied the first time when the value does not exist in the file, because when the path already exists it is not a default value anymore, so if you want to preserve the quote style you can use the addDefault
or setDefaults
as usual like in my previous message and then a function to set the quote style of your desired values for already existing values, without changing their values:
private static void wrapValues(YamlFile yamlFile, QuoteStyle quoteStyle, String... paths) {
for (String path : paths) {
if (yamlFile.isSet(path)) {
yamlFile.set(path, yamlFile.get(path), quoteStyle);
}
}
}
wrapValues(yamlFile, QuoteStyle.DOUBLE, "default");
Or you can use a custom method to set defaults, probably this is what you mean by a workaround:
private static <T> void setDefault(YamlFile yamlFile, String path, T value, QuoteStyle quoteStyle) {
yamlFile.set(path, yamlFile.isSet(path) ? yamlFile.get(path) : value, quoteStyle);
}
setDefault(yamlFile, "default", "test", QuoteStyle.DOUBLE);
Instead of using static functions you could also extend YamlFile
with your own methods.
from simple-yaml.
Related Issues (20)
- Load YamlConfiguration Error HOT 1
- Key setting with single quote despite no special characters. HOT 10
- Header being duplicated when there is no comment on the root key HOT 3
- There is a bug when i set the list HOT 2
- NoSuchMethodError when registering config HOT 6
- Invalid configuration caused by the library itself? HOT 3
- addDefault doesn't work since 1.8 if file contains only defaults HOT 5
- Bump up of SnakeYML to 1.31. HOT 1
- (Velocity dependency clash) java.lang.NoSuchMethodError HOT 13
- java.lang.NoSuchMethodError: 'void org.yaml.snakeyaml.DumperOptions.setIndentWithIndicator(boolean)' HOT 3
- Could not find artifact me.carleslc.Simple-YAML:Simple-Configuration:jar:1.8.3 in central (https://repo1.maven.org/maven2) HOT 6
- [Question] String length limit! Can it be disabled? HOT 3
- Update snakeyaml version to 2.0 HOT 1
- The method for saving the config is not working. HOT 2
- ConfigurationSection#addDefault changes values if present HOT 2
- Custom Domain Jitpack Deployment HOT 5
- String List placement inside yaml HOT 10
- YamlFile#load & #loadWithComments behave different on double quote value HOT 6
- Memory Leak Problem HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-yaml.