Comments (3)
Customize the snakeyaml dumper options
There are two options available in the snakeyaml DumperOptions
that you can configure for this use case:
setWidth
(80 by default): the desired line length. If the string is larger, it is split into multiple lines (no new line is included in the string itself, only in the yaml file).
setSplitLines
(true by default): If this setting is false, then the width is ignored and strings will not be split no matter its length.
These options are not present in the yamlFile.options()
but all configurations related to the snakeyaml implementation are available through yamlFile.getImplementation()
.
As you already guessed, you need to cast to either SimpleYamlImplementation
or SnakeYamlImplementation
to get access to the getDumperOptions
method.
DumperOptions yamlOptions = ((SimpleYamlImplementation) yamlFile.getImplementation()).getDumperOptions();
yamlOptions.setWidth(100);
Using literal strings
You can also use literal strings, which are not split by width like plain scalar strings.
String longLine = "This is a very long string line that we want to be dumped in a single line in the yaml file. To achieve this behaviour, we configure the line width and split lines in the dumper options, or dump this as a literal string.";
yamlFile.set("long-line-100", longLine); // QuoteStyle.PLAIN by default, with the width 100 set above
yamlFile.set("long-line-literal", longLine, QuoteStyle.LITERAL);
long-line-100: This is a very long string line that we want to be dumped in a single line in the yaml
file. To achieve this behaviour, we configure the line width and split lines in the dumper options,
or dump this as a literal string.
long-line-literal: |-
This is a very long string line that we want to be dumped in a single line in the yaml file. To achieve this behaviour, we configure the line width and split lines in the dumper options, or dump this as a literal string.
Literal strings are split only if new lines \n
are actually present in the provided string.
If needed, to always use literal style with strings you can set it to be the string default instead of QuoteStyle.PLAIN
:
yamlFile.options().quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.LITERAL);
yamlFile.set("long-line-literal", longLine); // Now QuoteStyle.LITERAL is the default
Providing your own configured implementation
Another way to customize the dumper options is to create a custom YamlImplementation
that inherits SimpleYamlImplementation
, overriding the configure
method to apply your own values.
public class CustomYamlConfiguration extends SimpleYamlImplementation {
@Override
public void configure(YamlConfigurationOptions options) {
// Don't forget this line to apply default configurations first!
super.configure(options);
// Customize options to your choice
options.quoteStyleDefaults().setQuoteStyle(String.class, QuoteStyle.DOUBLE);
// Customize dumper options to your choice
DumperOptions yamlOptions = this.getDumperOptions();
yamlOptions.setSplitLines(false);
}
}
Then, to create a YamlFile
using this implementation, you can override the default implementation:
YamlFile yamlFile = new YamlFile("config.yml");
yamlFile.setImplementation(new CustomYamlConfiguration());
Or just use the implementation constructor:
YamlFile yamlFile = new YamlFile(new CustomYamlConfiguration());
yamlFile.setConfigurationFile("config.yml"); // Set your yml file here (also available with File or URI)
// Then, set up your file as usual without worrying about configuration options here
yamlFile.createOrLoad();
String longLine = "This is a very long string line that we want to be dumped in a single line in the yaml file. To achieve this behaviour, we configure the line width and split lines in the dumper options, or dump this as a literal string.";
yamlFile.set("long-line", longLine);
yamlFile.save();
long-line: "This is a very long string line that we want to be dumped in a single line in the yaml file. To achieve this behaviour, we configure the line width and split lines in the dumper options, or dump this as a literal string."
This is somewhat advanced but is the optimal way if you have several configuration options to customize.
Hope it helps to you or other users with similar use cases.
More details about implementations can be found here.
from simple-yaml.
Ok, found out!
SnakeYamlImplementation implementation = (SnakeYamlImplementation) yamlFile.getImplementation();
implementation.getDumperOptions().setSplitLines(false);
This is the way!
The rest of the DumpOptions can be found at https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation#markdown-header-dumping-a-custom-yaml-document
from simple-yaml.
Thank You very much for all the in-deep details and recomendations :D
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
- Enhancement/Feature Request - addDefault() enforce quote style HOT 3
- 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
- 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.