推荐风格

2022.06.17

本文按照YAML官方文档1.2.2版本翻译总结而成

https://yaml.org/spec/1.2.2/#chapter-10-recommended-schemas

A YAML schema is a combination of a set of tags and a mechanism for resolving non-specific tags.

10.1. Failsafe Schema

The failsafe schema is guaranteed to work with any YAML document. It is therefore the recommended schema for generic YAML tools. A YAML processor should therefore support this schema, at least as an option.

10.1.1. Tags

10.1.1.1. Generic Mapping

Example 10.1 !!map Examples

10.1.1.2. Generic Sequence

Example 10.2 !!seq Examples

10.1.1.3. Generic String

Example 10.3 !!str Examples

10.1.2. Tag Resolution

All nodes with the “!” non-specific tag are resolved, by the standard convention, to “tag:yaml.org,2002:seq”, “tag:yaml.org,2002:map” or “tag:yaml.org,2002:str”, according to their kind.

All nodes with the “?” non-specific tag are left unresolved. This constrains the application to deal with a partial representation.

10.2. JSON Schema

The JSON schema is the lowest common denominator of most modern computer languages and allows parsing JSON files. A YAML processor should therefore support this schema, at least as an option. It is also strongly recommended that other schemas should be based on it.

10.2.1. Tags

The JSON schema uses the following tags in addition to those defined by the failsafe schema:

10.2.1.1. Null

Example 10.4 !!null Examples

10.2.1.2. Boolean

Example 10.5 !!bool Examples

10.2.1.3. Integer

Example 10.6 !!int Examples

10.2.1.4. Floating Point

Example 10.7 !!float Examples

10.2.2. Tag Resolution

The JSON schema tag resolution is an extension of the failsafe schema tag resolution.

All nodes with the “!” non-specific tag are resolved, by the standard convention, to “tag:yaml.org,2002:seq”, “tag:yaml.org,2002:map” or “tag:yaml.org,2002:str”, according to their kind.

Collections with the “?” non-specific tag (that is, untagged collections) are resolved to “tag:yaml.org,2002:seq” or “tag:yaml.org,2002:map” according to their kind.

Scalars with the “?” non-specific tag (that is, plain scalars) are matched with a list of regular expressions (first match wins, e.g. 0 is resolved as !!int). In principle, JSON files should not contain any scalars that do not match at least one of these. Hence the YAML processor should consider them to be an error.

Regular expressionResolved to tag
nulltag:yaml.org,2002:null
true | falsetag:yaml.org,2002:bool
-? ( 0 | [1-9] [0-9]* )tag:yaml.org,2002:int
-? ( 0 | [1-9] [0-9]* ) ( \. [0-9]* )? ( [eE] [-+]? [0-9]+ )?tag:yaml.org,2002:float
*Error

Note: The regular expression for float does not exactly match the one in the JSON specification, where at least one digit is required after the dot: ( \. [0-9]+ ). The YAML 1.2 specification intended to match JSON behavior, but this cannot be addressed in the 1.2.2 specification.

Example 10.8 JSON Tag Resolution

A null: null Booleans: [ true, false ] Integers: [ 0, -0, 3, -19 ] Floats: [ 0., -0.0, 12e03, -2E+05 ] Invalid: [ True, Null, 0o7, 0x3A, +12.3 ]{ "A null": null, "Booleans": [ true, false ], "Integers": [ 0, 0, 3, -19 ], "Floats": [ 0.0, -0.0, 12000, -200000 ], "Invalid": [ "True", "Null", "0o7", "0x3A", "+12.3" ] }
  

10.3. Core Schema

The Core schema is an extension of the JSON schema, allowing for more human-readable presentation of the same types. This is the recommended default schema that YAML processor should use unless instructed otherwise. It is also strongly recommended that other schemas should be based on it.

10.3.1. Tags

The core schema uses the same tags as the JSON schema.

10.3.2. Tag Resolution

The core schema tag resolution is an extension of the JSON schema tag resolution.

All nodes with the “!” non-specific tag are resolved, by the standard convention, to “tag:yaml.org,2002:seq”, “tag:yaml.org,2002:map” or “tag:yaml.org,2002:str”, according to their kind.

Collections with the “?” non-specific tag (that is, untagged collections) are resolved to “tag:yaml.org,2002:seq” or “tag:yaml.org,2002:map” according to their kind.

Scalars with the “?” non-specific tag (that is, plain scalars) are matched with an extended list of regular expressions. However, in this case, if none of the regular expressions matches, the scalar is resolved to tag:yaml.org,2002:str (that is, considered to be a string).

Regular expressionResolved to tag
null | Null | NULL | ~tag:yaml.org,2002:null
/* Empty */tag:yaml.org,2002:null
true | True | TRUE | false | False | FALSEtag:yaml.org,2002:bool
[-+]? [0-9]+tag:yaml.org,2002:int (Base 10)
0o [0-7]+tag:yaml.org,2002:int (Base 8)
0x [0-9a-fA-F]+tag:yaml.org,2002:int (Base 16)
[-+]? ( \. [0-9]+ | [0-9]+ ( \. [0-9]* )? ) ( [eE] [-+]? [0-9]+ )?tag:yaml.org,2002:float (Number)
[-+]? ( \.inf | \.Inf | \.INF )tag:yaml.org,2002:float (Infinity)
\.nan | \.NaN | \.NANtag:yaml.org,2002:float (Not a number)
*tag:yaml.org,2002:str (Default)

Example 10.9 Core Tag Resolution

A null: null Also a null: # Empty Not a null: "" Booleans: [ true, True, false, FALSE ] Integers: [ 0, 0o7, 0x3A, -19 ] Floats: [ 0., -0.0, .5, +12e03, -2E+05 ] Also floats: [ .inf, -.Inf, +.INF, .NAN ]{ "A null": null, "Also a null": null, "Not a null": "", "Booleans": [ true, true, false, false ], "Integers": [ 0, 7, 58, -19 ], "Floats": [ 0.0, -0.0, 0.5, 12000, -200000 ], "Also floats": [ Infinity, -Infinity, Infinity, NaN ] }
  

10.4. Other Schemas

None of the above recommended schemas preclude the use of arbitrary explicit tags. Hence YAML processors for a particular programming language typically provide some form of local tags that map directly to the language’s native data structures (e.g., !ruby/object:Set).

While such local tags are useful for ad hoc applications, they do not suffice for stable, interoperable cross-application or cross-platform data exchange.

Interoperable schemas make use of global tags (URIs) that represent the same data across different programming languages. In addition, an interoperable schema may provide additional tag resolution rules. Such rules may provide additional regular expressions, as well as consider the path to the node. This allows interoperable schemas to use untagged nodes.

It is strongly recommended that such schemas be based on the core schema defined above.