I do not know how many of you are familiar with BARE encoding format which was designed by Drew DeVault. It is super simple format for binary encoding of messages with structure known ahead-of-time (so it isn’t self-descriptive like CBOR). Now it got Erlang implementation (currently without compiler for building structure definition form text definitions).
Library usage is pretty simple (here with Elixir syntax):
spec = {:struct, [foo: :uint, bar: :string]}
encoded = :bare.encode(%{foo: 10, bar: "Hełło"}, spec)
:bare.decode(encoded, spec)
#=> {:ok, %{foo: 10, bar: "Hełło"}, <<>>}
Supported types are:
-
:uint
/:int
- variable width unsigned/signed integer (specs limit size of these integers to max 64-bit integers) -
:u8
/:u16
/:u32
/:u64
- fixed width unsigned integers -
:i8
/:i16
/:i32
/:i64
- fixed width signed integers -
:bool
- boolean values (akatrue
andfalse
atoms) -
:f32
/:f64
- floating points of given size, currently encoding is limited to finite values, but encoding supports not-finite values which are returned as atoms -
{:enum, [atom(), ...]}
- which will encode known set of atoms asuint
-
:data
anddata<N>
- which will encode arbitrary/fixed-length binaries -
:string
- UTF-8 encoded chartist or binary -
:void
- empty type that can encode only[]
-
{:optional, type}
- optional value oftype
-
{:array, type}
- list oftype
-
{:array, type, size}
- fixed length list oftype
-
{:map, key_type, value_type}
- map with keys ofkey_type
and values ofvalue_type
-
{:union, [type(), ...]}
- tagged union of types which can encode values in form of{type(), data}
wheredata
is oftype()
-
{:struct, keyword(type())}
- structure with given keys that can encode maps with atom keys and values of specified types
Let me know what you think about it, either here or via mailing list.
In future I plan to implement web tokens with BARE and PASETO to make web tokens implementation that for me will be near ideal solution. I will ping you back when I will have something done.