Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Beyond XML and JSON: YAML for Java Developers

Get a high-level introduction to the YAML file format for Java developers. Find out how it differs from XML and JSON and examine its relative advantages and drawbacks.


advertisement
espite all the buzz generated by dynamic languages (Ruby, Groovy, Python, etc.) and their related frameworks (such as Ruby on Rails), the vast majority of Java developers reading this article deal mostly with pure Java at their day jobs and will continue to do so for many years to come. However, that doesn't mean that they can't learn something from the new kids on the block and add a new tool to their arsenals. This article introduces the YAML (short for YAML Ain't Markup Language) file format (popularized by the Ruby on Rails framework, which uses YAML for all of its configuration files) and shows how it differs from XML and JSON. It goes on to examine YAML's advantages and drawbacks.

Whitespace Indentation and the JSON Option
The YAML file format is centered on the concept of whitespace indentation, which is used to indicate the hierarchical structure of data—instead of nested XML tags or JSON braces ({}) and brackets ([]). It is, however, a superset of JSON. So when it is useful, you may break out of the whitespace flow and adopt a typical JSON-style syntax. Its creators describe it as a "human-friendly data serialization standard for all programming languages." In my experience, its focus on "human-friendliness" is what sets it apart.

Here's a sample of pure YAML using whitespace indentation. Fixed width fonts are critical when creating YAML files, because spacing is crucial (see Sidebar 1. YAML and Tabs):



JFrame: defaultCloseOperation: JFrame.EXIT_ON_CLOSE title: Test Frame width: 800 height: 400 components: - JTextArea: name: textArea1 text: | This is a really long text that spans multiple lines (but preserves new lines). It does not need to be escaped with special brackets, CDATA tags, or anything like that - JButton: name: button2 text: Button 2

Depending on your needs, you can switch at any point to JSON-style syntax and mix and match it with whitespace indentation, for example:

JFrame: defaultCloseOperation: JFrame.EXIT_ON_CLOSE title: Test Frame width: 800 height: 400 components: - JTextArea: name: textArea1 text: | This is a really long text that spans multiple lines (but preserves new lines). It does not need to be escaped with special brackets, CDATA tags, or anything like that - JButton: {name: button2, text: Button 2} #JSON syntax

The ability to switch to JSON-style syntax is especially useful on the lowest-level nodes (i.e., those that have no children of their own). Also, as you've probably guessed, the pound sign (#) is used to embed comments into a YAML file.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap