#
ドキュメント

Document

自分のための備忘録です。

YAML

Ref

YAMLとJSON

  • YAMLJSONのスーパーセット
  • JSONでできることはYAMLでできる

基本

What is YAML?

システム間での可搬性を維持するために、タブ文字は設計上許可されていないため、代わりに空白文字 (リテラルスペース文字) を使用します。

YAML 初心者によくある質問は「3 つのダッシュは何を意味するのか」ですが、3 つのダッシュ (---) はドキュメントの開始を示すために使用し、各ドキュメントの終了を示すには 3 つのドット (...) を使用します。

#Comment: This is a supermarket list using YAML
#Note that - character represents the list
---
food: 
  - vegetables: tomatoes #first list item
  - fruits: #second list item
      citrics: oranges 
      tropical: bananas
      nuts: peanuts
      sweets: raisins

基本サンプル(YAMLとJSON)

employees:
  - yamada
  - tanaka
  - sato
{
  "employees": [
    "yamada", 
    "tanaka", 
    "sato"
  ]
}
employees:
  - employee1: '0001' # ''で囲まないと0001 は 1と解釈
    name: yamada
  - employee2: '0002'
    name: tanaka
  - employee3: '0003'
    name: sato
{
  "employees": [
    {
      "employee1": "0001", 
      "name": "yamada"
    }, 
    {
      "employee2": "0002", 
      "name": "tanaka"
    }, 
    {
      "employee3": "0003", 
      "name": "sato"
    }
  ]
}

複数行

  • >または|を使用

複数行サンプル

引用元:https://qiita.com/jerrywdlee/items/d5d31c10617ec7342d56

foo: |
  bar
  baz
{
  "foo": "bar\nbaz"
}
foo: >
  bar
  baz
{
  "foo": "bar baz"
}

アンカー&エイリアス(Anchors and alias)

To DRY up your config.yml, use anchors and aliases. Anchors are identified by an & character, and aliases by an * character.

https://circleci.com/docs/2.0/writing-yaml/

song:
  - &name Al
  - You
  - can
  - call
  - me
  - *name
song:
  - Al
  - You
  - can
  - call
  - me
  - Al

サンプル

引用:https://circleci.com/docs/2.0/writing-yaml/

default: &default
  school: hogwarts

harry:
  <<: *default
  house: gryffindor
{
  "default": {
    "school": "hogwarts"
  },
  "harry": {
    "house": "gryffindor", 
    "school": "hogwarts"
  }
}
name: &harry_name
  first_name: Harry
  last_name: Potter

address: &harry_address
  street: 4, Privet Drive
  district: Little Whinging
  county: Surrey
  country: England

harry_data:
  <<: [*harry_name, *harry_address]

<<: 配列はマージする。

{
  "harry_data": {
    "county": "Surrey", 
    "first_name": "Harry", 
    "last_name": "Potter", 
    "street": "4, Privet Drive", 
    "district": "Little Whinging", 
    "country": "England"
  }, 
  "name": {
    "first_name": "Harry", 
    "last_name": "Potter"
  }, 
  "address": {
    "county": "Surrey", 
    "country": "England", 
    "street": "4, Privet Drive", 
    "district": "Little Whinging"
  }
}

Yaml to JSON サンプル

例1

x: "x"
y: "y"
z:
 - zX: 'zX'
   zY: 'zY'
 - zX: 'zX'
   zY: 'zY'
{
  "x": "x",
  "y": "y",
  "z": [
    {
      "zX": "zX",
      "zY": "zY"
    },
    {
      "zX": "zX",
      "zY": "zY"
    }
  ]
}

例2

vertexes:
  num: 15
  digit: 3
  nodes:
    - name: "a"
      position:
        x: 10
        y: 20
        z: 30
      color: "red"
    - name: "b"
      position:
        x: 15
        y: 8
        z: 30
      color: "blue"
{
  "vertexes": {
    "num": 15,
    "digit": 3,
    "nodes": [
      {
        "name": "a",
        "position": {
          "x": 10,
          "y": 20,
          "z": 30
        },
        "color": "red"
      },
      {
        "name": "b",
        "position": {
          "x": 15,
          "y": 8,
          "z": 30
        },
        "color": "blue"
      }
    ]
  }
}