混合使用 YAML 和 JSON

huangapple go评论87阅读模式
英文:

Mixing yaml and json

问题

YAML 是 JSON 的超集,这意味着我可以直接将 JSON 放入一个 .yml 文件中,它就能工作。

我想主要编写 JSON,但对于我的代码块,切换到 YAML 字面样式会很不错。

Chat Gpt 说我可以这样做(但它是错误的,尽管这个想法对我来说似乎是有效的):

{
    "name": "John Doe",
    "description": |
        这是一个多行
        使用 YAML 字面样式的描述
        在 JSON 文件中。        
    "age": 30
}

“纯值不能以块标量指示符 | 开头,位于第3行,第20列:”

有人知道如何利用 YAML 是 JSON 超集的事实来允许这种混合匹配行为吗?

英文:

Yaml is a superset of json meaning I can literally just slap json into a .yml file and it works.

I would like to mostly write json, but for my code blocks it would be great to switch to the yaml literal style.

Chat Gpt Said I can do this, (but it's wrong though the idea seems valid to me)

{
    "name": "John Doe",
    "description": |
        This is a multi-line
        description using the YAML
        literal style in a JSON file.
    "age": 30
}

Plain value cannot start with block scalar indicator | at line 3, column 20:

Does anyone know a way to use the fact that yaml is a superset of json to allow this kind of mix and match behavior?

答案1

得分: 1

YAML有两种节点样式,block(块样式)和flow(流样式),通常使用block样式:

--- # 块映射
foo: bar
droggel: jug

--- # 块序列
- foo
- bar

--- # 流映射
{ foo: bar, droggel: jug }

--- # 流序列
[ foo, bar ]

--- # 流标量
droggeljug

--- # 块标量
|
    droggeljug

块样式使用缩进来定义结构和嵌套,而流样式使用显式的特殊字符。

在块样式内可以使用流样式,这是有效的:

foo: [droggel, jug]

但是,流样式不允许在块样式内部使用。我不知道这个决定的具体原因,但这可能会使语法变得更加复杂,这似乎是一个可能的原因。

所以答案是:你不能在流映射中使用块标量。如果你想使用块标量,那么祖先节点也必须是块样式。然而,你可以在流样式中使用兄弟节点,所以下面这个是有效的:

name: "John Doe"
description: |
        This is a multi-line
        description using the YAML
        literal style in a JSON file.        
other: {
  age: 30,
  }

(提示:YAML规范要求关闭的 } 要比封闭块节点的缩进更深。但是,实现通常允许你将 } 放在第一列。我在这里进行了缩进以符合规范的要求)。

英文:

YAML has two node styles, block and flow, with block being the one that's usually used:

--- # block mapping
foo: bar
droggel: jug

--- # block sequence
- foo
- bar

--- # flow mapping
{ foo: bar, droggel: jug }

--- # flow sequence
[ foo, bar ]

--- # flow scalar
droggeljug

--- # block scalar
|
    droggeljug

Block style uses indentation to define structure and nesting, while flow style uses explicit special characters.

Flow style is allowed within block style, making this valid:

foo: [droggel, jug]

However, block style is not allowed within flow style. I don't know what the reasoning for this decision was but it would make the syntax more complex than it already is, which seems a likely reason.

So the answer to your question is: You can't use a block scalar in a flow mapping. If you want to use a block scalar, are ancestor nodes must also be in block style. You can, however, give sibling nodes in flow style, so this would be valid:

name: "John Doe"
description: |
        This is a multi-line
        description using the YAML
        literal style in a JSON file.        
other: {
  age: 30,
  }

(Hint: the YAML spec requires the closing } to be more indented than the enclosing block node. But implementations usually do allow you to put the } on the first column. I indented it here for total spec conformance).

huangapple
  • 本文由 发表于 2023年4月20日 00:20:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定