Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Beyond XML and JSON: YAML for Java Developers : Page 3

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.


WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

As you can plainly see from the examples so far, YAML is noticeably less verbose than XML. Most of a YAML file's content is the actual data, not endless lists of opening and closing tags, which themselves are often larger that the data they describe. As such, YAML is much better suited for any sort of data file that you may need to maintain by hand.

On the downside, YAML does not provide the concept of a schema or DTD, so there is no way to verify whether the format of the file is what you expected. XML's verbosity has its costs, but the overall maturity of that format provides a lot of extra tools for validation that YAML does not have (yet).

JSON is perfect for any data that is geared towards efficiency and reducing file size, because it wastes almost no space on whitespace or closing tags. However, as the content of a JSON file increases in complexity, it descends into closing-bracket hell. This is most painfully visible in JavaFX code (which is based around JSON). A UI structure that is any more complex results in a data file that becomes nearly incomprehensibly complex towards the end.

Look at this JavaFX code sample (click the "Edit this page" link) and pay particular attention to how it ends:

} } } } ] } } center: bookPanel }

The mix of structural "{}" and list "[]" brackets makes maintaining large JSON-style files by hand quite difficult. YAML solves this issue quite neatly with its whitespace indentation approach, while still allowing you to switch to a JSON-style flow whenever it is acceptable (such as on bottom-level nodes).

YAML Java Libraries and Development Tools
The most popular Java library for processing YAML files is JvYAML. JRuby (the Ruby version that runs as a dynamic language on the Java VM) uses JvYAML in its port of the Ruby on Rails framework. JvYAML provides facilities to perform generic processing of a file (in which case it returns a nested hierarchy of standard Java String, Long, Map, and List objects). You save a file using the static dump() method, and load one via the static load() method, for example:

YAML.dump(Object data, Writer output); Object data = YAML.load(Reader io);

See Sidebar 2. Open Source Project Based on YAML for information about a YAML-based open source project for building declarative UIs in Java.

In reality, YAML files are usually so simple that they you can easily maintain them with any text editor. However, a few specialized text editors provide helpful syntax highlighting. For Eclipse, it's the Eclipse YAML editor. For NetBeans, you can use the YAML editor that comes with the Ruby pack. However, the YAML editor in NetBeans 6.1 is not very useful; it supports only a small subset of YAML (e.g., it does not support block literals). This is supposed to be fixed by the new YAML editor coming in NetBeans 6.5.

I did not have a chance to test IntelliJ IDEA, but I presume its Ruby on Rails plugin ships with a YAML editor.

Time to Add the YAML Tool
The overly verbose XML format is overkill in most cases. YAML and its usage from Java could be very useful alternatives on your next project. For further study, visit the YAML page at Wikipedia (which has an excellent overview of advanced YAML features, such as data merging and data casting), as well as the official YAML site.

Jacek Furmankiewicz is a Senior Java EE and Erlang/OTP developer at Radialpoint. He has 15 years of professional IT experience and is a Sun Certified Java Programmer (SCJP).
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date