不同的 YAML 数组表示 [英] Different YAML array representations

查看:40
本文介绍了不同的 YAML 数组表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 PyYAML 为一个项目编写文件类型转换器,我在该项目中多次与 YAML 文件相互转换.这些文件随后由我无法控制的单独服务使用,因此我需要将 YAML 翻译回与我最初获得的相同.我的原始文件包含以下部分:

I'm writing a file type converter using Python and PyYAML for a project where I am translating to and from YAML files multiple times. These file are then used by a separate service that I have no control over, so I need to translate back the YAML the same as I originally got it. My original file has sections of the following:

key:
- value1
- value2
- value3

使用 yaml.load() 计算结果为 {key: [value1,value2,value3]}.当我将其转换回 YAML 时,我的新文件如下所示:

Which evaluates to {key: [value1,value2,value3]} using yaml.load(). When I translate this back to YAML my new file reads like this:

key: [value1,value2,value3]

我的问题是,就 YAML 文件的各种语言解析器而言,这两种形式是否等效.显然,使用 PyYaml,这些是等效的,但是对于应用程序使用的 Ruby 或其他语言,这是否适用?否则,应用程序将无法正确显示数据.

My question is whether these two forms are equivalent as far as the various language parsers of YAML files are concerned. Obviously using PyYaml, these are equivalent, but does this hold true for Ruby or other languages, which the application is using? If not, then the application will not be able to display the data properly.

推荐答案

是的,对于任何遵循规范的 YAML 解析器,它们都是等效的.您可以在此处阅读规范:http://www.yaml.org/spec/1.2/规范.html

Yes, to any YAML parser that follows the spec, they are equivalent. You can read the spec here: http://www.yaml.org/spec/1.2/spec.html

第 3.2.3.1 节特别相关(重点是我的):

Section 3.2.3.1 is particularly relevant (emphasis mine):

每个节点都以某种风格呈现,具体取决于其种类.节点样式是表示细节,不会反映在序列化树或表示图中. 有两组样式.块样式使用缩进来表示结构;相比之下,流样式依赖于明确的指标.

3.2.3.1. Node Styles

Each node is presented in some style, depending on its kind. The node style is a presentation detail and is not reflected in the serialization tree or representation graph. There are two groups of styles. Block styles use indentation to denote structure; In contrast, flow styles styles rely on explicit indicators.

澄清一下,节点是 YAML 中的任何结构,包括数组(在规范中称为序列).单线样式称为流序列(参见第 7.4.1 节),多线样式称为块序列(第 8.2.1 节).兼容的解析器会将两者反序列化为相同的对象.

To clarify, a node is any structure in YAML, including arrays (called sequences in the spec). The single-line style is called a flow sequence (see section 7.4.1) and the multi-line style is called a block sequence (section 8.2.1). A compliant parser will deserialize both into identical objects.

这篇关于不同的 YAML 数组表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆