YAML流樣式

YAML中的流樣式可以被認為是JSON的自然擴展,以覆蓋折疊內容行以獲得更好的可讀特徵,該特徵使用錨點和別名來創建對象實例。 在本章中,將重點關注以下概念的流表示

  • 別名節點
  • 空節點
  • 流標量樣式
  • 流集合樣式
  • 流節點

別名節點的示例如下所示 -

%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"
]

-


上一篇: YAML塊標量頭 下一篇: YAML塊樣式