使用 Yaml folded > string 表示 A JSON 字符串会导致添加意外的换行符
Representing a A JSON String using Yaml folded > string results in adding unexpected newline characters
使用 YAML folded > string 表示 A JSON 字符串会导致添加意外的换行符。是否可以表示示例字符串而不在折叠字符串的每一行之后引入换行符?
- yaml: >-
{
"This" : "is supposed to be a JSON string.",
"it" : "is not meant to be a yaml map itself.",
"And": "if this string were passed into"
"a": "json parser, it would THEN parse as a JSON map."
"The": "confusing part to me is the newlines."
"Normally": ">- folded style strings do not introduce"
"newline": "characters between lines within the yaml"
"folded": "There shouldn't be any newlines added."
"But": "several unexpected newlines are introduced."
"Does": "This have something to do with the special"
"characters": "in the string?"
}
这是生成的字符串,在解析时在折叠的 >- 样式字符串的每一行末尾引入了一个换行符。
[ {
"yaml": "{\n "This" : "is supposed to be a JSON string.",\n "it" : "is not meant to be a yaml map itself.",\n "And": "if
this string were passed into"\n "a": "json parser, it would THEN
parse as a JSON map."\n "The": "confusing part to me is the
newlines."\n "Normally": ">- folded style strings do not
introduce"\n "newline": "characters between lines within the
yaml"\n "folded": "There shouldn't be any newlines added."\n
"But": "several unexpected newlines are introduced."\n "Does":
"This have something to do with the special"\n "characters": "in
the string?"\n}" } ]
根据我一直在阅读的规范,这不应该发生。其他折叠的字符串不会发生这种情况,但对于这个例子,可能与特殊字符有关。
尽管我已经看到它在至少两个 YAML 解析器(Python 的 pyyaml 和 Java 的 snake YAML)中执行此操作。也出现在这个yaml parser webapp.
要么我误解了规范,要么库都错误地实现了它(也许是故意的,为了彼此兼容)。
最终,我问是因为我想将 YAML 用于项目中的配置文件。但我担心我将无法完全按照我想要的方式表示多行字符串(不引入意外的空格、换行符等)
您链接的规范部分直接解决了这个问题:
Lines starting with white space characters (more-indented lines) are not folded.
规范中的示例 8.10 和 8.11 显示了如何不折叠更多缩进行。在您的 YAML 代码中,除了 {
和 }
之外的所有内容都更加缩进,因此,这些行没有折叠。
背景是折叠块标量想让你拥有像项目符号列表这样的东西,例如
content: >
foo
* one
* two
bar
并且这些项目符号应该分别解析为整行,因此建立了不折叠更多缩进行的规则。事实证明,该规则对 YAML 出现的用例相当不利,如您的示例所示。
如果您不希望出现这种情况,我建议您像这样定义本地标签:
content: !fold |
foo
* one
* two
bar
然后您可以为您的标签编写一个自定义构造函数,在最初展开的文字块标量内容上按照您希望的方式进行行折叠。
使用 YAML folded > string 表示 A JSON 字符串会导致添加意外的换行符。是否可以表示示例字符串而不在折叠字符串的每一行之后引入换行符?
- yaml: >-
{
"This" : "is supposed to be a JSON string.",
"it" : "is not meant to be a yaml map itself.",
"And": "if this string were passed into"
"a": "json parser, it would THEN parse as a JSON map."
"The": "confusing part to me is the newlines."
"Normally": ">- folded style strings do not introduce"
"newline": "characters between lines within the yaml"
"folded": "There shouldn't be any newlines added."
"But": "several unexpected newlines are introduced."
"Does": "This have something to do with the special"
"characters": "in the string?"
}
这是生成的字符串,在解析时在折叠的 >- 样式字符串的每一行末尾引入了一个换行符。
[ { "yaml": "{\n "This" : "is supposed to be a JSON string.",\n "it" : "is not meant to be a yaml map itself.",\n "And": "if this string were passed into"\n "a": "json parser, it would THEN parse as a JSON map."\n "The": "confusing part to me is the newlines."\n "Normally": ">- folded style strings do not introduce"\n "newline": "characters between lines within the yaml"\n "folded": "There shouldn't be any newlines added."\n "But": "several unexpected newlines are introduced."\n "Does": "This have something to do with the special"\n "characters": "in the string?"\n}" } ]
根据我一直在阅读的规范,这不应该发生。其他折叠的字符串不会发生这种情况,但对于这个例子,可能与特殊字符有关。
尽管我已经看到它在至少两个 YAML 解析器(Python 的 pyyaml 和 Java 的 snake YAML)中执行此操作。也出现在这个yaml parser webapp.
要么我误解了规范,要么库都错误地实现了它(也许是故意的,为了彼此兼容)。
最终,我问是因为我想将 YAML 用于项目中的配置文件。但我担心我将无法完全按照我想要的方式表示多行字符串(不引入意外的空格、换行符等)
您链接的规范部分直接解决了这个问题:
Lines starting with white space characters (more-indented lines) are not folded.
规范中的示例 8.10 和 8.11 显示了如何不折叠更多缩进行。在您的 YAML 代码中,除了 {
和 }
之外的所有内容都更加缩进,因此,这些行没有折叠。
背景是折叠块标量想让你拥有像项目符号列表这样的东西,例如
content: >
foo
* one
* two
bar
并且这些项目符号应该分别解析为整行,因此建立了不折叠更多缩进行的规则。事实证明,该规则对 YAML 出现的用例相当不利,如您的示例所示。
如果您不希望出现这种情况,我建议您像这样定义本地标签:
content: !fold |
foo
* one
* two
bar
然后您可以为您的标签编写一个自定义构造函数,在最初展开的文字块标量内容上按照您希望的方式进行行折叠。