Article · Mecha CMS

Updates.

YAML Parser

Author: Taufik Nurrohman · 795 Views

Tweet

YAML-like string parser.

To store array in YAML format or to convert YAML-like data format into array is possible with Mecha but it is limited only to key: value-based syntax to mimic array keys and their values. This means that you can’t parse external YAML files anyway with Mecha’s internal string parser, but the YAML-like data generated by To::yaml() method basically can be treated as a valid YAML data in most cases.

Converter 

String to Array 

$test = From::yaml('foo: bar');

Related: From

Array to String 

$test = To::yaml(['foo' => 'bar']);

Related: To

Syntax 

Key and Value 

foo: bar
baz: qux
{
  "foo": "bar",
  "baz": "qux"
}

Empty Value 

foo:
bar: ''
baz: ""
{
  "foo": [],
  "bar": "",
  "baz": ""
}

Empty Data 

[]

Comments 

# comment
test_1: foo
test_2: foo # comment
test_3: 'foo # comment' # comment
test_4: "foo # comment" # comment
test_5: # comment 'foo'
test_6: # comment "foo"
{
  "test_1": "foo",
  "test_2": "foo",
  "test_3": "foo # comment",
  "test_4": "foo # comment",
  "test_5": [],
  "test_6": []
}

Sequential Array 

Arrays with numeric keys.

Manual 
0: a
1: b
2: c
["a", "b", "c"]
Automatic 
- a
- b
- c
["a", "b", "c"]

Compact Array 

foo: [bar, baz, 0, true]
bar: { baz: qux }
{
  "foo": ["bar", "baz", 0, true],
  "bar": {
    "baz": "qux"
  }
}

Data Type 

All data types will be converted automatically by the e.

string: a
integer: 1
float: 1.5
boolean: true
null: null
"YAML's null": ~
JSON: {"foo":"bar","baz":"qux","string_integer":"1","string_boolean":"false"}
string_integer: "1"
string_float: "1.5"
string_boolean: "true"
string_null: "null"
single_quote: 'foo\nbar'
double_quote: "foo\nbar"
{
  "string": "a",
  "integer": 1,
  "float": 1.5,
  "boolean": true,
  "null": null,
  "YAML's null": null,
  "JSON": {
    "foo": "bar",
    "baz": "qux",
    "string_integer": "1",
    "string_boolean": "false"
  },
  "string_integer": "1",
  "string_float": "1.5",
  "string_boolean": "true",
  "string_null": "null",
  "single_quote": "foo\\nbar",
  "double_quote": "foo\nbar"
}
[
    'string' => 'a',
    'integer' => 1,
    'float' => 1.5,
    'boolean' => true,
    'null' => null,
    'YAML\'s null' => null,
    'JSON' => stdClass::__set_state([
        'foo' => 'bar',
        'baz' => 'qux',
        'integer' => 1,
        'boolean' => false
    ]),
    'string_integer' => '1',
    'string_float' => '1.5',
    'string_boolean' => 'true',
    'string_null' => 'null',
    'single_quote' => 'foo\nbar',
    'double_quote' => 'foo
bar'
]

Nesting 

a: b
c: d
e:
  f: g
  h: i
j: k
{
  "a": "b",
  "c": "d",
  "e": {
    "f": "g",
    "h": "i"
  },
  "j": "k"
}

Escaping 

foo\: bar: baz
foo: bar: baz
{
  "foo: bar": "baz",
  "foo": "bar: baz"
}
"foo: bar": baz
foo: bar: baz
{
  "foo: bar": "baz",
  "foo": "bar: baz"
}
'foo: bar': baz
foo: bar: baz
{
  "foo: bar": "baz",
  "foo": "bar: baz"
}

Block 

folded block: >
  lorem ipsum
  dolor sit amet
    a b c
literal block: |
  lorem ipsum
  dolor sit amet
    a b c
{
  "folded block": "lorem ipsum dolor sit amet\n  a b c",
  "literal block": "lorem ipsum\ndolor sit amet\n  a b c"
}

Reference 

copy 1: &ref1 foo, bar, baz
copy 2: &ref2
  foo: bar
  baz: qux
paste 1: *ref1
paste 2: *ref2
{
  "copy 1": "foo, bar, baz",
  "copy 2": {
    "foo": "bar",
    "baz": "qux"
  },
  "paste 1": "foo, bar, baz",
  "paste 2": {
    "foo": "bar",
    "baz": "qux"
  }
}

0001000000

0 Comments

No comments yet.