如何保存yaml中的所有前导缩进



我希望用户能够指定块标量样式字符串,并保留他们指定的所有前导缩进。

电流输入

SomeText: |
This is some text that I would like for
The leading indents to be preserved.
It would be really nice for the leading indents to be preserved.

预期输出

This is some text that I would like for
The leading indents to be preserved.
It would be really nice for the leading indents to be preserved.

实际输出

This is some text that I would like for
The leading indents to be preserved.
It would be really nice for the leading indents to be preserved.

正如您所看到的,当我在Java中使用YAML库解析YAML时,前导缩进没有保留,但我希望保留它们我该怎么做

我想我需要使用块缩进指示符,但YAML规范太令人困惑了。有人能解释一下假人的块缩进指示器吗?如有任何帮助,我们将不胜感激。

使用块缩进指示符:

SomeText: |1
This is some text that I would like for
The leading indents to be preserved.
It would be really nice for the leading indents to be preserved.

这明确指定了标量内容相对于周围缩进的缩进,即在这种情况下,块标量的缩进被定义为比SomeText:多1。第一个空格之后的所有内容都被解释为内容,而不是缩进,并且将是标量值的一部分。

请注意,缩进指示符必须至少为1,因为块标量的缩进必须比周围的缩进至少多1,因为它将以任何具有周围缩进(或更小(的内容结束。您永远不能保留被解释为缩进的空白(这与缩进的想法相反(,但您仍然可以通过添加空白在行前放置任意数量的空白。

最新更新