#! /usr/bin/env ruby
require 'json'
require 'yaml'
print YAML::load(STDIN.read).to_json
print "\n"
$ cat sequence.yml
---
- a
- b
- 10
$ cat sequence.yml | ./yaml2json.rb
["a","b",10]
---
- a
- b
- c
...
---
- 1
- 2
- 3
...
"..." は省略可# comment - data # comment
- a - b - 10ハイフン(-) の後にスペース1文字以上必要
[a,b,10]
- a - - b-1 - b-2 - cスペース1文字以上のインデント。tabはダメ
key1 : value1 key2 : value2
{key1 : value1, key2 : value2}
key1 : value1 key2 : value2 parent : chkey1 : chval1 chkey2 : chval2
---
- "no mark" :
- George Bernard Shaw :
He who can, does.
He who cannot, teaches.
- "|" :
- George Bernard Shaw : |
He who can, does.
He who cannot, teaches.
- "|+" :
- George Bernard Shaw : |+
He who can, does.
He who cannot, teaches.
- "|-" :
- George Bernard Shaw : |-
He who can, does.
He who cannot, teaches.
- ">" :
- George Bernard Shaw : >
He who can, does.
He who cannot, teaches.
- ">+" :
- George Bernard Shaw : >+
He who can, does.
He who cannot, teaches.
- ">-" :
- George Bernard Shaw : >-
He who can, does.
He who cannot, teaches.
評価結果
$ cat cr.yml | ./yaml2json.rb
[{"no mark": [{"George Bernard Shaw": "He who can, does. He who cannot, teaches." }]},
{"|": [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches.\n" }]},
{"|+": [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches.\n" }]},
{"|-": [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches." }]},
{">": [{"George Bernard Shaw": "He who can, does. He who cannot, teaches.\n" }]},
{">+": [{"George Bernard Shaw": "He who can, does. He who cannot, teaches.\n" }]},
{">-": [{"George Bernard Shaw": "He who can, does. He who cannot, teaches." }]}
]
YAML中の改行 | 末尾の改行 | |
no mark | スペース | 無視 |
"|" | 改行 | 改行 |
">" | スペース | 改行 |
"+" 末尾に改行を付ける "-" 末尾の改行を無視
---
- servers :
- &web
name : turip
ip : 192.168.10.31
- &db
name : pansy
ip : 192.168.10.32
- services :
- nginx :
server : *web
port : 80
- redis :
server : *web
port : 61
- mapserver :
server : *db
port : 8080
- postgis :
server : *db
port : 5432
評価結果
$ cat anchor.yml | ./yaml2json.rb
[{"servers":[{"name":"turip","ip":"192.168.10.31"},{"name":"pansy","ip":"192.168.10.32"}]},
{"services":[
{"nginx":{"server":{"name":"turip","ip":"192.168.10.31"},"port":80}},
{"redis":{"server":{"name":"turip","ip":"192.168.10.31"},"port":61}},
{"mapserver":{"server":{"name":"pansy","ip":"192.168.10.32"},"port":8080}},
{"postgis":{"server":{"name":"pansy","ip":"192.168.10.32"},"port":5432}}
]}
]
アンカーの内容に key を付けたくないときには、<<: で単純マージができる
---
- servers :
- &web
name : turip
ip : 192.168.10.31
- &db
name : pansy
ip : 192.168.10.32
- services :
- nginx :
<< : *web
port : 80
- redis :
<< : *web
port : 61
- mapserver :
<< : *db
port : 8080
- postgis :
<< : *db
port : 5432
評価結果
$ cat anchor2.yml | ./yaml2json.rb
[{"servers":[{"name":"turip","ip":"192.168.10.31"},{"name":"pansy","ip":"192.168.10.32"}]},
{"services":[
{"nginx":{"name":"turip","ip":"192.168.10.31","port":80}},
{"redis":{"name":"turip","ip":"192.168.10.31","port":61}},
{"mapserver":{"name":"pansy","ip":"192.168.10.32","port":8080}},
{"postgis":{"name":"pansy","ip":"192.168.10.32","port":5432}}
]}
]