Ruby: Yaml vs XML

I found myself in need of some data serialization methods this evening and checked out Yaml which is billed as an “easy” way of saving an object (say a query result) and then later re-constituting that information back into something useful. Turns out this is a surprisingly difficult thing to do presently in Ruby as [...]

I found myself in need of some data serialization methods this evening and checked out Yaml which is billed as an “easy” way of saving an object (say a query result) and then later re-constituting that information back into something useful. Turns out this is a surprisingly difficult thing to do presently in Ruby as there have been some changes in behavior to the YAML libraries that complicate things.

I ended up just using XML, which is generally considered to be “heavier”, but for now at least is much easier to get working. If you’re unfamiliar with these constructs I’ve put an example below, these are the same two records, one as YAML, one as XML.

YAML
- !ruby/object:Watchlist
attributes:
name: macromedia
id: "287"
last_updated: 2007-10-22 13:05:27
- !ruby/object:Watchlist
attributes:
name: adobe air
id: "288"
last_updated: 2007-10-22 13:05:29

XML
<watchlist>
<id type=“integer”>287id>
<last-updated type=“datetime”>2007-10-22T13:05:27-04:00last-updated>

<name>macromedianame>
watchlist>

<watchlist>
<id type=“integer”>288id>

<last-updated type=“datetime”>2007-10-22T13:05:29-04:00last-updated>
<name>adobe airname>

watchlist>

For more on the reasons behind the current difficulties with YAML (at least when supporting multiple platforms of Ruby), checkout this post on CFIS about YAML troubles.

For an example of how easy it is to parse XML with Ruby, checkout this article on YDN: HOWTO Parse XML using Ruby

One Comment

  1. jre2 added these pithy words on July 11, 2008 | Permalink

    I can’t say for certain about ruby’s libraries, but in pyyaml, custom data types such as:

    - !ruby/object:Watchlist

    can be shortened (or even inferred via some regex) with 1-2 lines of code.

POST A COMMENT

Your email is never published nor shared. Required fields are marked *

*
*