ErlAMF is an AMF(Action Message Format) encoding/deconding library written in Erlang.
ErlAMF uses rebar3 as build tool.
# build
$ git clone git://github.com/sile/erl-amf.git
$ cd erl-amf
$ rebar3 compile
# run unit tests
$ rebar3 eunit
$ erl -pa ebin
%%%
%%% Encode and Decode
%%%
%%% AMF3 encode
1> {ok, IoList} = amf:encode(amf3, [1,2,3]).
{ok,[9,7,[1],[[4,1],[4,2],[4,3]]]}
%% AMF3 decode
2> amf:decode(amf3, list_to_binary(IoList)).
{ok,[1,2,3],<<>>}
%%%
%%% AMF values
%%%
%% Load records
%% (Records are defined in "erl-amf/include/amf.hrl")
3> rr(amf).
[amf_array,amf_avmplus_object,amf_byte_array,amf_date,
amf_dictionary,amf_exception,amf_object,amf_vector,amf_xml,
amf_xml_document]
%% Anonymous object
4> amf:object([{<<"one">>, 1}, {<<"two">>, 2}]).
#amf_object{class = undefined,dynamic = true,
sealed_fields = [],
members = [{<<"one">>,1},{<<"two">>,2}]}
5> amf:encode(amf0, v(4)).
{ok,[3,
[<<0,3>>, <<"one">>,
[0,<<63,240,0,0,0,0,0,0>>],
<<0,3>>, <<"two">>,
[0,<<64,0,0,0,0,0,0,0>>]],
0,0,9]}
%% Date
6> amf:encode(amf0, amf:date(now())).
{ok,[11,<<66,115,221,102,242,183,208,0,0,0>>]}
%%%
%%% Error
%%%
%% The encode/2(and decode/2) function returns an amf_exception() instance as error reason
7> amf:encode(amf0, can_not_encode).
{error,#amf_exception{type = unsupported,
message = {value,can_not_encode}}}
8> amf:decode(amf0, <<>>).
{error,#amf_exception{type = partial,
message = {marker,<<>>}}}
Erlang Value | AMF0 Marker | AMF3 Marker |
---|---|---|
10 | number | integer |
10.5 | number | double |
true | boolean | true |
false | boolean | false |
null | null | null |
undefined | undefined | undefined |
<<"string">> | string | long-string | string |
movieclip | ||
recordset | ||
unsupported | ||
amf:date(now()) | date | date |
amf:xml(<<"xml">>) | xml | |
amf:xml_document(<<"xml">>) | xml-document | xml-doc |
amf:byte_array(<<"bytes">>) | byte-array | |
amf:vector(int, [-1,2,-3]) | vector-int | |
amf:vector(uint, [1,2,3]) | vector-uint | |
amf:vector(double, [-1.0,2.0,-3.0]) | vector-double | |
amf:vector(<<"type_name">>, [1,<<"2">>,[3]]) | vector-object | |
[1,2,3] | strict-array | array (only dense element) |
amf:array([{<<"key">>, <<"value">>}]) | ecma-array | array (only associative element) |
amf:array([1,2,3], [{<<"key">>, <<"value">>}]) | array (mixed) | |
amf:object([{<<"key">>, <<"value">>}]) | object | object (anonymous) |
amf:typed_object(<<"type">>, [{<<"key">>, <<"value">>}]) | typed-object | object (typed) |
amf:dictionary([{1,2}, {true, false}]) | dictionary | |
amf:avmplus_object([1,2,3]) | avmplus-object |
ErlAMF doesn't support following features:
- Circular reference objects
- flash.utils.IExternalizable interface
- No Object/Trait/String reference is used in encoding process
- Special double value (e.g. NaN, Infinity)