Continuing the discussion on how to make Erlang more Ajax friendly, I would like to share the format I use for representation of Erlang terms in my Flex/Actionscript-based X Erlang IDE.
The format codenamed as ASON (just like JSON, but for Actionscript) allows representation of every possible Erlang term in Actionscript (again funs are somewhat of a hassle) and is built upon Actonscript's support for inline XML syntax (e4x).
1. Atoms, numbers and strings encoded similarily
<value atom="abc" />
<value string="def" />
<value number="123" />
1. Lists and tuples use XML nesting
<list>
<value numbe="1" />
<value numbe="2" />
<value numbe="3" />
</list>
<list />
// []
<tuple>
<value numbe="1" />
<value numbe="2" />
<value numbe="3" />
</tuple>
3. Binaries use XML text entity
<binary>010203ff</binary>
// means <<1,2,3,255>>
4. Pids, ports and references are alikes
<pid node="local" serial="1" creation="0" />
<port node="local" serial="1" creation="0" />
<ref node="local" serial="1" creation="0" />
Now, I can call an Erlang function from Actionscript like this:
Erlang.apply("lists:nth", <list><value number="2" />
<list><value atom="a" /><value atom="b" /><value atom="c" /></list>
</list>, onResult);
function onResult(success:Number, result:XML):void
{
Alert.show("lists:nth(2, [a,b,c]) returns "+result.@atom+".");
}
Of course, there are easier ways to retrieve the second element of the list in Actionscript.
The format codenamed as ASON (just like JSON, but for Actionscript) allows representation of every possible Erlang term in Actionscript (again funs are somewhat of a hassle) and is built upon Actonscript's support for inline XML syntax (e4x).
1. Atoms, numbers and strings encoded similarily
<value atom="abc" />
<value string="def" />
<value number="123" />
1. Lists and tuples use XML nesting
<list>
<value numbe="1" />
<value numbe="2" />
<value numbe="3" />
</list>
<list />
// []
<tuple>
<value numbe="1" />
<value numbe="2" />
<value numbe="3" />
</tuple>
3. Binaries use XML text entity
<binary>010203ff</binary>
// means <<1,2,3,255>>
4. Pids, ports and references are alikes
<pid node="local" serial="1" creation="0" />
<port node="local" serial="1" creation="0" />
<ref node="local" serial="1" creation="0" />
Now, I can call an Erlang function from Actionscript like this:
Erlang.apply("lists:nth", <list><value number="2" />
<list><value atom="a" /><value atom="b" /><value atom="c" /></list>
</list>, onResult);
function onResult(success:Number, result:XML):void
{
Alert.show("lists:nth(2, [a,b,c]) returns "+result.@atom+".");
}
Of course, there are easier ways to retrieve the second element of the list in Actionscript.
No comments:
Post a Comment