Nov 23
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.

February 11th, 2008 at 11:19 pm
Hi Derek, great post and awsome work!
I had bit of problem with getting it to work:
I am using Flash CS3 so the first problem was in
Serializer.as
import mx.utils.StringUtil;
this is a flex package so i had to change it to
import com.adobe.utils.StringUtil;
in order for it to find the class
but now I get:
Serializer.as line 139
1061: Call to a possibly undefined method substitute through a reference with static type Class.
it’s at this line:
return StringUtil.substitute(this.anchorTemplate, [new int(this.lastAnchorId)]);
please help I couldn’t find any “substitute” method in com.adobe.utils.StringUtil
how can i get this to work?
thanks alot for your help
February 12th, 2008 at 2:26 am
Hi Bachir,
Well, you could try to find an open source library that contains that method, or you could write it yourself. It’s pretty straight-forward with regular expressions. This is essentially what you need.
public function substitute(string: String, …args): String
{
return string = string.replace(/{(\d+)}/g, function(a,b,c,d):String{return args[b]});
}
Add some error checking and throw this in a utility class and you should be good to go.
February 12th, 2008 at 11:17 am
Thanks for the prompt response Derek.
Ok I have downloaded Flex3_SDK packages and classes so it’s all good using the utils classes.
Another interesting error I got is:
VerifyError: Error #1107: The ABC data is corrupt, attempt to read out of bounds.
at Function/http://adobe.com/AS3/2006/builtin::call()
at org.as3yaml::Parser/parseStreamNext()
at org.as3yaml::Parser/peekEvent()
at org.as3yaml::Composer/composeNode()
at org.as3yaml::Composer/composeDocument()
at org.as3yaml::Composer/getNode()
at org.as3yaml::BaseConstructor/getData()
at org.as3yaml::YAML$/load()
at org.as3yaml::YAML$/decode()
at Example_fla::MainTimeline/onYamlLoad()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
had a look around and bumped to this thread:
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=72&catid=616&threadid=1323788&enterthread=y
I believe sooner or later it will happen to everyone.
Note sure about the workaround.
Any thoughts.
thanks again for your time and great share of your work.
February 15th, 2008 at 5:34 am
Hi Derek after further research:
I found :
[..]
it seems that error was being generated in response to an in-line event handler that was named in its in-line definition. I was able to resolve the issue by either defining the event listener function outside of the listener declaration OR by removing the function name from the in-line definition.
[...]
seems it’s a known issue:
https://bugs.adobe.com/jira/browse/SDK-14054
maybe that will help. (you’re probably aware of all that)
but I thought I’d leave it to the expert.
thanks
February 15th, 2008 at 10:48 am
to summarize this,
I got it working if compiled in flex3 SDK, but when compiling using Flash CS3 IDE i keep getting this “ABC…” error.
hopefully Adobe will get that sorted.
thanks alot for as3yaml!
February 19th, 2008 at 1:19 pm
hi Derek,
I was wondering if it is an expected behavior for the flash player to freeze during parsing large yaml file. I have noticed this is happening when Yaml.decode is at work.
even a looping preloader animation stops from playing until parsing is complete(about 10 seconds)
Is there any tips or tricks to have get around this?
please help only if you got some time.
thx
March 5th, 2008 at 10:19 pm
great library, nice to see an usage of the as3commons lib.
greetings
steffen
May 8th, 2008 at 2:00 am
Hi Derek just to confirm the last changes, for whoever is using the latest library, the use has changed since the decoding returns a Dictionary instead of a HashMap.
so the use is:
var yamlDict : Dictionary = YAML.decode(event.target.data) as Dictionary; // returns a Dictionary
trace(yamlMap.Date); // returns a Date object and prints: Fri Nov 23 15:03:17 GMT-0500 2001
trace(yamlMap.User); // returns a String and prints: ed
trace(yamlMap.Fatal); // returns a String and prints: Unknown variable “bar”
trace(yamlMap.Stack); // returns an Array and prints: [object Dictionary],[object Dictionary]
trace(yamlMap.Stack[0].line); // returns an Int and prints: 23
trace(yamlMap.Stack[0].code); // returns a String and prints: x = MoreObject(”345\n”)
will wait for Derek to confirm.
Thanks Derek for the performance in decoding is super fast now.
May 25th, 2008 at 8:29 pm
This would be awesome if it wasn’t dependent on the Flex library.
May 28th, 2008 at 2:56 am
Hey Jonathan –
Just to be clear. as3yaml only uses some of the utilities that are found in the mx packages (Base64Decoder, etc). I suppose I could have rewritten these classes, but I don’t see any reason to since Flex is open source and is freely available.
May 28th, 2008 at 2:58 am
Hey Bachir,
You are correct, and thank you for posting this info. Everyone should refer to:http://as3yaml.googlecode.com/svn/trunk/docs/org/as3yaml/YAML.html#decode()
June 3rd, 2008 at 4:35 am
could u tell me how to use ffmpeg format or any other which gives me facility to convert uploaded video to flv from flex to rails folder.
abhishekchess1@gmail.com
September 14th, 2008 at 3:13 pm
where I can use this library???
February 12th, 2009 at 4:09 pm
[...] flexonrails.net » as3yaml – A YAML 1.1 parser and emitter for ActionScript 3 (tags: actionscript a) [...]
February 20th, 2009 at 6:56 pm
Can you provide a version that is not dependent on Flex? I know Flex is open source but there’s a certification issue involved– it would be much better for my group if there were a non-Flex version. Thanks for your consideration… and for your YAML work of course! Cheers!
May 19th, 2009 at 4:44 pm
A non flex Yaml Loader/Emitter would rox.
May 24th, 2009 at 6:43 am
[...] a great parser and emitter coded in AS3 that you can download here and read more here. Unfortunately, I was only able to compile it using the Flex SDK, so I modified it a bit for Flash [...]