Binson implementation in Erlang.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE
README.md
binson.erl
binson.ex
binson_eqc.erl
binson_nif.c
binson_nif.erl
build.sh

README.md

binson-erlang

This repo is an implementation of Binson (see binson.org) in Erlang.

  • Binson module for Erlang.
  • Binson module for Elixir.
  • Erlang QuickCheck-mini model for binson-c-light testing using Erlang Binson implementation as reference.

Binson module for Erlang

Encoding function:

binson:encode(Value).

returns binary data.

Decoding function:

binson:decode(Binary).

returns decoded value and unparsed rest of binary input (if any).

Example:

sylbar@xubuntu-AA:~/binson-erlang$ erl
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Eshell V8.3  (abort with ^G)
1> c(binson).
{ok,binson}
2> B = binson:encode("abc").
<<20,3,97,98,99>>
3> binson:decode(B).
{"abc",<<>>}
4> 

Matching Binson types to Erlang:

Bool -> true/false atoms, example: true.
Integer -> integer type, example: 1.
Float -> float type, example: 1.0.
String -> Erlang strings (char. lists), example: "abc".
Bytes -> Erlang binaries, example: <<1,2,3>>.
Array -> Erlang tuples, example: {1, "abc", {"sub", 1.0}}.
Object -> Erlang maps, example: #{"field1" => "A", "field2" => {1,2,3}}.

Binson module for Elixir

Encoding function:

Binson.encode(value)

returns binary data.

Decoding function:

Binson.decode(binary)

returns decoded value and unparsed rest of binary input (if any).

Example:

sylbar@xubuntu-AA:~/binson-erlang$ iex
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("binson.ex")    
[Binson]
iex(2)> b = Binson.encode("abc")
<<20, 3, 97, 98, 99>>
iex(3)> Binson.decode(b)
{"abc", ""}
iex(4)> 

Matching Binson types to Elixir:

Bool -> true/false atoms, example: true.
Integer -> integer type, example: 1.
Float -> float type, example: 1.0.
String -> Elixir strings (binaries), example: "abc".
Bytes -> Elixir binaries in :bin tagged tuple, example: {:bin, <<1,2,3>>}.
Array -> Elixir lists, example: [1, "abc", ["sub", 1.0]].
Object -> Elixir maps, example: %{"field1" => "A", "field2" => [1,2,3]}.

How to run Erlang QuickCheck test?

  • Install Erlang.

  • Install Erlang QuickCheck mini from http://www.quviq.com/downloads/

  • Compile:

      ~/binson-erlang$ ./build.sh
    
  • Execute tests in Erlang shell:

      ~/binson-erlang/build$ erl
      Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]
      Eshell V8.3  (abort with ^G)
      1> binson_eqc:writer_test(100).
      ...
      2> binson_eqc:parser_test(100).
      ...