I am using Snakeyaml in Java to dump a Yaml-File which is later parsed by another Application.
The output-Yaml has to look like this:
aKey: 1234
anotherKey: a string
thirdKey:
4321:
- some script code passed as a string
- a second line of code passed as a string
So far, the closest i have come is by setting the dumper - options like this:
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
[...]
Map<String, Object> data = new LinkedHashMap<>();
Map<Integer, Object> thirdKey= new LinkedHashMap<>();
data.put("aKey", 1234);
data.put("anotherKey", "aString");
StringBuilder extended = new StringBuilder();
extended.append("- some script code passed as a string\n- a second line of code passed as a string\n");
String result = extended.toString();
thirdKey.put(4321, result);
data.put("thirdKey", thirdKey);
yaml.dump(data, writer);
The output, however, is like this:
aKey: 1234
anotherKey: a string
thirdKey:
4321: |
- some script code passed as a string
- a second line of code passed as a string
The mere | is what is separating me from achieving my final goal.
Is there any way to set the dumperoptions to break the lines like this without inserting a | symbol?
The problem is that you generate the list markers -
as part of your content. They are not! They are part of the YAML syntax.
Since what you want in YAML is a sequence (YAML term for a list), you have to put a list into your structure:
thirdKey.put(4321, Arrays.asList(
"some script code passed as a string",
"a second line of code passed as a string"));