A Whistlestop Tour of YAML Grammar
You can read the full YAML specification
, but here's a very brief introduction to YAML's grammar. Sequences of information are separated by a dash and a space '- ', as follows:
Mappings, which associate one piece of information with another, use a colon and a space ': ' as shown below:
You can easily associate sequences with a piece of information:
- Freddy Adu
- Troy Perkins
- Earnie Stewart
- Eddie Pope
- Eddie Gaven
- Jonny Walker
Or you can create a sequence of value mappings:
Name: Freddy Adu
Name: Earnie Stewart
These few examples just scratch the surface of the grammar set for YAML, but they should suffice to give you a good feel for the sheer simplicity of the language, and how easily human-readable it is.
A Use Case for YAML
By now you've heard of how Web services, using XML as their binding, allow disparate clients on different machines to come together to form distributed applications. The key to this is that Web services use XML for the data and XML/SOAP for the control of that data. In many cases, particularly for smaller, simpler applications, a Web service is overkill, but you'll probably still use one because the nice thing about XML is that it is easy to generate, and you don't need to write a parser to accept it. There are parsers available for every programming language and operating system that you can think of!
Similarly, for YAML to work, you need to have a YAML parser that can handle this serializable data format, or, why would you bother? It would be much easier to write a custom data format optimized for your needs than it would be to write a YAML parser. Fortunately, you don't have to, because there are YAML parsers out there, in various states of construction, for most programming languages, with .NET being the exception.
At this moment in time, YAML is probably best supported in Ruby, thanks to the Syck parser, which is lightweight, rapid, and built in with later versions of the language. Syck is also available for PHP, and there are open-source parsers for many other languages. For this article I used the Java language parser, available at http://homepages.ihug.com.au/~zenaan/zenaan/files/
. Interestingly enough, if you want to use .NET, the Java Language Conversion Assistant from Microsoft converts this parser quite painlessly, although you should get in contact with the source owner for permission to distribute it if you take that route.
|Figure 1. The Time Series Database: The figure shows a Query Window view of the PriceHistory.Prices database table.|
The rest of this article describes how to build a simple PHP YAML-over-HTTP service that reads data from a MySQL database and renders it in YAML. A Java client can then read this YAML with the aid of the parser noted above.
Included with the downloadable code
is a SQL script called CreatePriceHistory.sql
that creates and populate a database with thirty days' worth of time-series data for the stock symbol MSFT. Figure 1
shows the database table structure generated by the CreatePriceHistory.sql