Recipe 13.1. Serializing Data with YAMLProblemYou want to serialize a data structure and use it later. You may want to send the data structure to a file, then load it into a program written in a different programming language. SolutionThe simplest way is to use the built-in
Arrays are represented as bulleted lists:
Hashes are represented as colon-separated key-value pairs:
More complex Ruby objects are represented in terms of their classes and member variables:
You can dump a data structure to a file with YAML.dump, and load it back with YAML.load:
DiscussionIf you've ever used Python's pickle module or serialized a Java object, you know how convenient it is to be able to dump an object to disk and load it back later. You don't have to define a custom data format or write an XML generator: you just shove the object into a file or a database, and read it back later. The only downside is that the serialized file is usually a binary mess that can only be understood by the serialization library. You can also use YAML to serialize Ruby-specific objects: symbols, ranges, and regular expressions. Indeed, you can use YAML to serialize instances of custom classes: YAML serializes the class of the object and the values of its instance variables. There's no guarantee, though, that other programming languages will understand what you mean.[3]
Not only is YAML human-readable, it's human-writable. You can write YAML files in a text editor and load them into Ruby as objects. If you're having trouble with the YAML representation of a particular data structure, your best bet is to define a simple version of that data structure in an irb session, dump it to YAML, and work from there.
Before you get drunk with power, you should know that YAML shares the limitations of other serialization schemes. Most obviously, you can only deserialize objects in an environment like the one in which you serialized them. Suppose you convert a Set object to YAML in one Ruby session:
In another Ruby session, you might try to convert the YAML back into a Set, without first requiring the set library:
Instead of a Set, you've got an unresolved object of class YAML::Object. The set has been loaded from the file and deserialized, but Ruby can't resolve its class name. YAML can only serialize data; it can't serialize Ruby code or system resources (such as filehandles or open sockets). This means some objects can't be fully converted to YAML. The following code successfully serializes and deserializes a File object, but the deserialized File isn't open and doesn't point to anything in particular:
The essence of the File objectits handle to a file on disk, granted by the operating systemhas been lost. Objects that contain Ruby code will lose their code when dumped to YAML. This means that Proc and Binding objects will turn up empty. Objects with singleton methods will be dumped without them. Classes can't be dumped to YAML at all. But these are all edge cases. Most data structures, even complex ones, can be serialized to See Also
|
Wednesday, November 18, 2009
Recipe 13.1. Serializing Data with YAML
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment