Nov 23 2007
as3yaml – A YAML 1.1 parser and emitter for ActionScript 3
I am pleased to announce the first release of as3yaml, an ActionScript 3 library for parsing and emitting YAML. It is a direct port of Ola Bini’s jvyaml, which was itself a port of Kirill Simonov’s PyYAML.
For those of you who are unfamiliar with YAML, here is a concise description from the yaml.org welcome page:
YAML(tm) (rhymes with “camel”) is a straightforward machine parsable data serialization format designed for human readability and interaction with scripting languages such as Perl and Python. YAML is optimized for data serialization, configuration settings, log files, Internet messaging and filtering. YAML(tm) is a balance of the following design goals:
- YAML documents are very readable by humans.
- YAML interacts well with scripting languages.
- YAML uses host languages’ native data structures.
- YAML has a consistent information model.
- YAML enables stream-based processing.
- YAML is expressive and extensible.
- YAML is easy to implement.
Here are some example of YAML taken from the current version of the YAML spec:
american: - Boston Red Sox - Detroit Tigers - New York Yankees national: - New York Mets - Chicago Cubs - Atlanta Braves - name: Mark McGwire hr: 65 avg: 0.278 - name: Sammy Sosa hr: 63 avg: 0.288
Here is an example of decoding a YAML string and converting it to ActionScript objects:
With the following YAML stored in a file called myYaml.yaml
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
Unknown variable "bar"
Stack:
- file: TopClass.py
line: 23
code: |
x = MoreObject("345\n")
- file: MoreClass.py
line: 58
code: |-
foo = bar
You can load then load the YAML and decode it as follows.
public function loadYaml() : void { var loader : URLLoader = new URLLoader(); loader.load(new URLRequest('myYaml.yaml')); loader.addEventListener(Event.COMPLETE, onYamlLoad); } public function onYamlLoad(event : Event) : void { var yamlMap : HashMap = YAML.decode(event.target.data) as HashMap; // returns a HashMap trace(yamlMap.get("Date")); // returns a Date object and prints: Fri Nov 23 15:03:17 GMT-0500 2001 trace(yamlMap.get("User")); // returns a String and prints: ed trace(yamlMap.get("Fatal")); // returns a String and prints: Unknown variable "bar" trace(yamlMap.get("Stack")); // returns an Array and prints: [object HashMap],[object HashMap] trace(yamlMap.get("Stack")[0].get("line")); // returns an Int and prints: 23 trace(yamlMap.get("Stack")[0].get("code")); // returns a String and prints: x = MoreObject("345\n") }
There are some more examples available in the as3yaml docs.
I have been working on this for a little while now, and running it through various tests, so I think it is at a point where it is ready to be released into the wild. Please submit any bugs that you find to the issues list on the google project site. If you have any interest in contributing to the project, please let me know.
THANKS:
- Of course, special thanks to Ola Bini and Kirill Simonov for their excellent work on jvyaml and PyYaml, respectively.
- I would also like to thank the as3commons project. This library made the port from Java quite a bit easier than it might have been.
