Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sile/erl-amf

An Erlang library to encode/decode AMF
https://github.com/sile/erl-amf

amf erlang

Last synced: 8 days ago
JSON representation

An Erlang library to encode/decode AMF

Awesome Lists containing this project

README

        

# ErlAMF

[![hex.pm version](https://img.shields.io/hexpm/v/amf.svg)](https://hex.pm/packages/amf)
[![Build Status](https://travis-ci.org/sile/amf.svg?branch=master)](https://travis-ci.org/sile/erl-amf)
[![Code Coverage](https://codecov.io/gh/sile/amf/branch/master/graph/badge.svg)](https://codecov.io/gh/sile/erl-amf/branch/master)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

ErlAMF is an AMF(Action Message Format) encoding/deconding library written in Erlang.

[EDoc Documentation](https://hexdocs.pm/amf/)

## Usage
### Build

ErlAMF uses [rebar3](https://github.com/erlang/rebar3) as build tool.

```sh
# build
$ git clone git://github.com/sile/erl-amf.git
$ cd erl-amf
$ rebar3 compile

# run unit tests
$ rebar3 eunit
```

### Examples

```erlang
$ 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,<<>>}}}
```

## Mapping between Erlang Values and AMF types(markers)

Erlang ValueAMF0 MarkerAMF3 Marker
10numberinteger
10.5numberdouble
truebooleantrue
falsebooleanfalse
nullnullnull
undefinedundefinedundefined
<<"string">>string | long-stringstring
movieclip
recordset
unsupported
amf:date(now())datedate
amf:xml(<<"xml">>)xml
amf:xml_document(<<"xml">>)xml-documentxml-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-arrayarray (only dense element)
amf:array([{<<"key">>, <<"value">>}])ecma-arrayarray (only associative element)
amf:array([1,2,3], [{<<"key">>, <<"value">>}])array (mixed)
amf:object([{<<"key">>, <<"value">>}])objectobject (anonymous)
amf:typed_object(<<"type">>, [{<<"key">>, <<"value">>}])typed-objectobject (typed)
amf:dictionary([{1,2}, {true, false}])dictionary
amf:avmplus_object([1,2,3])avmplus-object

## Limitations
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)

## Reference
* [AMF0 Specification](http://download.macromedia.com/pub/labs/amf/amf0_spec_121207.pdf)
* [AMF3 Specification](http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/amf/pdf/amf-file-format-spec.pdf)