YAML - JSON 模式



YAML 中的 JSON 模式被認為是大多數現代計算機語言的公分母。它允許解析 JSON 檔案。強烈建議在 YAML 中將其他模式視為 JSON 模式。這麼做的主要原因在於它包含使用者友好的鍵值組合。訊息可以編碼為鍵,並且可以在需要的時候使用。

JSON 模式是標量且沒有值。JSON 模式中的對映條目以某種鍵值對的形式表示,其中 null 被視為有效值。

示例

一個 null JSON 模式表示如下所示 −

!!null null: value for null key
key with null value: !!null null

JSON 表示的輸出如下所示 −

{
   "null": "value for null key", 
   "key with null value": null
}

示例

以下示例表示布林 JSON 模式 −

YAML is a superset of JSON: !!bool true
Pluto is a planet: !!bool false

以下是 JSON 格式的輸出 −

{
   "YAML is a superset of JSON": true, 
   "Pluto is a planet": false
}

示例

以下示例表示整數 JSON 模式 −

negative: !!int -12
zero: !!int 0
positive: !!int 34
生成的 JSON 模式的輸出如下所示
{
   "positive": 34, 
   "zero": 0, 
   "negative": -12
}

示例

JSON 模式中的標記表示如下示例所列 −

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 ]

JSON 輸出如下所示 −

{
   "Integers": [
      0, 
      0, 
      3, 
      -19
   ], 
   
   "Booleans": [
      true, 
      false
   ], 
   "A null": null, 

   "Invalid": [
         true, 
         null, 
         "0o7", 
         58, 
         12.300000000000001
   ], 
   
   "Floats": [
      0.0, 
      -0.0, 
      "12e03", 
      "-2E+05"
   ]
}
廣告
© . All rights reserved.