Yaml-flow-styles

提供:Dev Guides
移動先:案内検索

YAML-フロースタイル

Flow styles in YAML can be thought of as a natural extension of JSON to cover the folding content lines for better readable feature which uses anchors and aliases to create the object instances. In this chapter, we will focus on flow representation of the following concepts −

  • エイリアスノード
  • 空のノード
  • Flow Scalarスタイル
  • フロー収集スタイル *フローノード

エイリアスノードの例を以下に示します-

%YAML 1.2
---
!!map {
   ? !!str "First occurrence"
   : &A !!str "Foo",
   ? !!str "Override anchor"
   : &B !!str "Bar",
   ? !!str "Second occurrence"
   :* A,
   ? !!str "Reuse anchor"
   : *B,
}

上記のコードのJSON出力は以下のとおりです-

{
   "First occurrence": "Foo",
   "Second occurrence": "Foo",
   "Override anchor": "Bar",
   "Reuse anchor": "Bar"
}

コンテンツが空のノードは、空のノードと見なされます。 次の例はこれを示しています-

%YAML 1.2
---
!!map {
   ? !!str "foo" : !!str "",
   ? !!str "" : !!str "bar",
}

JSONの空のノードの出力は以下のように表されます-

{
   "": "bar",
   "foo": ""
}

フロースカラースタイルには、二重引用符、単一引用符、およびプレーンタイプが含まれます。 同じための基本的な例を以下に示します-

%YAML 1.2
---
!!map {
   ? !!str "implicit block key"
   : !!seq [
      !!map {
         ? !!str "implicit flow key"
         : !!str "value",
      }
   ]
}

上記の例のJSON形式での出力を以下に示します-

{
   "implicit block key": [
      {
         "implicit flow key": "value"
      }
   ]
}

YAMLのフローコレクションは、別のフローコレクション内のブロックコレクションにネストされています。 フローコレクションエントリは、カンマ)インジケータで終了します。 次の例は、フロー収集ブロックを詳細に説明します-

%YAML 1.2
---
!!seq [
   !!seq [
      !!str "one",
      !!str "two",
   ],

   !!seq [
      !!str "three",
      !!str "four",
   ],
]

JSONでのフロー収集の出力を以下に示します-

[
   [
      "one",
      "two"
   ],
   [
      "three",
      "four"
   ]
]

JSONのようなフロースタイルには、開始インジケーターと終了インジケーターが含まれます。 プロパティを持たない唯一のフロースタイルは、プレーンスカラーです。

%YAML 1.2
---
!!seq [
!!seq [ !!str "a", !!str "b" ],
!!map { ? !!str "a" : !!str "b" },
!!str "a",
!!str "b",
!!str "c",]

上記のJSON形式のコードの出力は以下のとおりです-

[
   [
      "a",
      "b"
   ],

   {
      "a": "b"
   },

   "a",
   "b",
   "c"
]