-
-
Notifications
You must be signed in to change notification settings - Fork 53
Folded and Literal Block Scalars
Put the bellow in a YAML file called scalars.yml
and read it
simple: plain_scalar
folded: >
a long line split into
several short
lines for readability
literal: |
line 1
line 2
line 3
final YamlMapping map = Yaml.createYamlInput("scalars.yml").readYamlMapping();
final String folded = map.foldedBlockScalar("folded");
final Collection<String> literalLines = map.literalBlockScalar("literal");
A Yaml scalar is basically a simple string. In the following example, "value" is what we call a Scalar.
key: value
The above is actually called a Plain Scalar, since it's a single sentence mapped to a key, no fancy stuff.
In YAML, you can also write Scalar values on multiple lines, and in this case it is called a Block Scalar. There are two types of Block Scalars:
folded: >
a long line split into
several short
lines for readability
The thing about Folded Block Scalars is that the new lines will not be taken into account, so when reading the above Scalar, the whole thing will be on a single line. Folded => "don't care about New Lines"
literal: |
line 1
line 2
line 3
In Literal Block Scalars the new lines are respected, so when you read it, you will get multiple lines. Literal => "DO care about New Lines"