I have been using YAML for awhile and decided I’d like to write a post about it. YAML is an acronym for YAML Ain’t Markup Language and it is a very useful and concise serialization standard. I use YAML all the time in when I am coding PHP projects that require some kind of configuration files.
Here is what wiki says about it:
YAML syntax was designed to be easily mapped to data types common to most high-level languages: list, hash, and scalar. Its familiar indented outline and lean appearance makes it especially suited for tasks where humans are likely to view or edit data structures, such as configuration files, dumping during debugging, and document headers (e.g. the headers found on most e-mails are very close to YAML). Although well-suited for hierarchical data representation, it also has a compact syntax for a relational data as well.
Here is a sample of what YAML looks like:
connection: "local_mysql"
widget:
type: "Table"
drawProperties:
title: "Syslog Table"
showRowNumber: "true"
allowHtml: "true"
pageSize: null
page: "disable"
#usually the table name
entityName: logs
entity:
table: logs
fields:
id:
field: id
type: number
counter:
field: counter
type: number
initialQuery: "select id, counter from logs order by id"
One of the great things about YAML is that it is very easy to read and takes much less syntax markup than XML. This enables you to crank out tight, clean configuration files very easily. When I load the previous YAML file up into PHP, here is the resulting data structure:
array(6) {
["connection"]=>
string(11) "local_mysql"
["widget"]=>
array(1) {
["type"]=>
string(5) "Table"
}
["drawProperties"]=>
array(4) {
["title"]=>
string(12) "Syslog Table"
["showRowNumber"]=>
string(4) "true"
["allowHtml"]=>
string(4) "true"
["page"]=>
string(7) "disable"
}
["entityName"]=>
string(4) "logs"
["entity"]=>
array(2) {
["table"]=>
string(4) "logs"
["fields"]=>
array(2) {
["id"]=>
array(2) {
["field"]=>
string(2) "id"
["type"]=>
string(6) "number"
}
["counter"]=>
array(2) {
["field"]=>
string(7) "counter"
["type"]=>
string(6) "number"
}
}
}
["initialQuery"]=>
string(40) "select id, counter from logs order by id"
}
The YAML file is actually parsed using a PHP YAML parser called Spyc. The syntax for Spyc is also quite simple:
$configArray = Spyc::YAMLLoad($yamlFile);
I was first introduced to YAML files when I began a Symfony project. Symfony makes heavy use of YAML files for all of its configuration. I really like the way Symfony was so configurable and have since added YAML config files to just about every project I work on. They are far easier to edit than XML, there is less overhead when parsing and apparently the YAML 1.2 specification is compatible with JSON which is amazing.
From wikipedia:
Both functionally and syntactically, JSON is effectively a subset of YAML. Specifically, as of YAML version 1.2, “every JSON file is also a valid YAML file”
Configuration files are a great way to make your life easier, and the life of anyone else who need to modify your code and YAML is a great place to start for creating these.
So YAML is a server side only format? Where you would convert to JSON on the client side? Why not just stick with JSON data?
I was saying that it appears in the latest specs that valid JSON is a subset of YAML which is interesting. Therefore a YAML parser should be able to parse JSON. Both are simply data serialization formats. I mean there isn’t too much right now that seems useful about this, but there is some interesting discussion if you search for YAML JSON. Personally I just use YAML for configuration, but I thought it was interesting enough to mention its relationship to JSON, that is all.