如何在不引入空白字符的情况下在 yaml 中将长字符串拆分为多行

How to split long strings over multiple lines in yaml without introducing whitespace characters

在 yaml 中,有多种方法可以将长字符串格式化为多行。例如:

输入:

example:
  string1: An example of a long string that is split across 
    two lines

输出(如JSON):

{
  "example": {
    "string1": "An example of a long string that is split across two lines"
   }
}

或使用显式折叠语法:

example:
  string1: >
    An example of a long string that is split across
    two lines

输出:

{
  "example": {
    "string1": "An example of a long string that is split across two lines"
   }
}

或使用文字语法:

example:
  string1: |
    An example of a long string that is split across
    two lines

输出:

{
  "example": {
    "string1": "An example of a long string that is split across\ntwo lines"
   }
}

在我能找到的所有示例中,生成的字符串似乎在每行之间插入一个 space 或一个换行符。但是,我想在几行中写一个很长的 yaml 字符串,结果行中没有插入 whitespace 。这可能吗?

我的要求:

example:
  string1: Oneverylongunbrokenlinethatmustnotcon
    tainanyresultingwhitespaceintheoutput

期望的输出:

{
  "example": {
    "string1": "Oneverylongunbrokenlinethatmustnotcontainanyresultingwhitespaceintheoutput"
   }
}

是否存在这样的语法?

您省略了双引号标量样式,这正是您所需要的:

example:
  string1: "Oneverylongunbrokenlinethatmustnotcon\
    tainanyresultingwhitespaceintheoutput"

双引号标量是 YAML 中唯一允许在任何位置分成多行的标量样式。这是通过转义换行符来完成的。如果你不逃避换行符,你会得到一个 space 就像大多数其他标量样式一样。

这不适用于任何其他样式,因为只有双引号标量处理转义序列。