阅读Yaml时如何保留前导空白

Gag*_*ing 3 java whitespace yaml indentation javabeans

我正在使用YamlReader(yamlbeans.YamlReader)阅读yaml文件

- tag: xyz
  description: |  
    This is multi-line comment and I want to preserve  
    leading white spaces and new line  
Run Code Online (Sandbox Code Playgroud)

当我阅读以下内容时:

String descr = tag.get("description");  
Run Code Online (Sandbox Code Playgroud)

它给出以下输出:

This is multi-line comment and I want to preserve  
leading white spaces and new line  
Run Code Online (Sandbox Code Playgroud)

但是我想保留领先的空白。

fly*_*lyx 6

使用缩进指示器:

- tag: xyz
  description: |1
     This is a multi-line comment and I want to preserve
     leading white spaces and new line
Run Code Online (Sandbox Code Playgroud)

1规定,下列块标量将有一个额外的缺口(除了当前的缩进级别),这会给空间:

  This is a multi-line comment and I want to preserve
  leading white spaces and new line
Run Code Online (Sandbox Code Playgroud)

如您所见,保留了块标量中一个缩进空间之后的两个空间。您可以使用任何一位数字作为缩进指示符。

如果要保留尾随换行符,请使用|1+,其中+告诉YAML保留尾随换行符。