2013-12-14 10:46:31 +00:00
|
|
|
# MessagePack Erlang
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2016-01-25 12:56:14 +00:00
|
|
|
[![Travis](https://secure.travis-ci.org/msgpack/msgpack-erlang.png)](https://travis-ci.org/msgpack/msgpack-erlang)
|
|
|
|
[![Drone.io](https://drone.io/github.com/msgpack/msgpack-erlang/status.png)](https://drone.io/github.com/msgpack/msgpack-erlang)
|
2016-03-22 15:26:59 +00:00
|
|
|
[![hex.pm version](https://img.shields.io/hexpm/v/msgpack.svg)](https://hex.pm/packages/msgpack)
|
2013-09-10 15:55:00 +00:00
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
## Prerequisites for runtime
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2016-02-16 08:32:35 +00:00
|
|
|
[Erlang/OTP](http://erlang.org/), >= 17.0 Also based on
|
|
|
|
[the new msgpack spec 0b8f5a](https://github.com/msgpack/msgpack/blob/0b8f5ac67cdd130f4d4d4fe6afb839b989fdb86a/spec.md).
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## edit rebar.config to use in your application
|
2013-09-09 13:03:44 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
|
|
|
{deps, [
|
|
|
|
{msgpack, ".*",
|
2015-12-08 06:05:57 +00:00
|
|
|
{git, "git://github.com/msgpack/msgpack-erlang.git", {branch, "master"}}}
|
2013-12-14 10:46:31 +00:00
|
|
|
]}.
|
|
|
|
```
|
2013-09-09 13:03:44 +00:00
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
Or as it is [now published at hex.pm](https://hex.pm/packages/msgpack), just
|
|
|
|
|
|
|
|
```erlang
|
|
|
|
{deps, [msgpack]}.
|
|
|
|
```
|
|
|
|
|
|
|
|
might work.
|
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## Simple deserialization
|
2013-09-09 13:03:44 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
|
|
|
Ham = msgpack:pack(Spam),
|
|
|
|
{ok, Spam} = msgpack:unpack(Ham).
|
|
|
|
```
|
2013-09-09 13:03:44 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## Stream deserialization
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
|
|
|
{Term0, Rest0} = msgpack:unpack_stream(Binary),
|
|
|
|
{Term1, Rest1} = msgpack:unpack_stream(Rest0),
|
|
|
|
...
|
|
|
|
```
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
## Options, for packing and unpacking
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
### `{spec, new|old}`
|
|
|
|
|
2016-02-16 09:49:34 +00:00
|
|
|
Both for packing and unpacking. Default is `new`. Major difference
|
|
|
|
between old and new spec is:
|
2016-02-16 07:47:41 +00:00
|
|
|
|
2016-02-16 08:32:35 +00:00
|
|
|
- raw family (`0xa0~0xbf`, `0xda`, `0xdb`) becomes new str family
|
|
|
|
- `0xd9` is new as str8
|
|
|
|
- new bin space (`0xc4, 0xc5, 0xc6` as bin8, bin16, bin32)
|
|
|
|
- new ext space (`0xc7, 0xc8, 0xc9` as ext8, ext16, ext32)
|
|
|
|
- new fixext space (`0xd4, 0xd5, 0xd6, 0xd7, 0xd8` as fixext1, fixext2, fixext4, fixext8, fixext16),
|
|
|
|
|
|
|
|
The default is new spec. Old spec mode does not handle these new types but
|
|
|
|
returns error. To use
|
|
|
|
[old spec](https://github.com/msgpack/msgpack/blob/master/spec-old.md)
|
|
|
|
mode, this option is explicitly added.
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
2016-02-16 08:32:35 +00:00
|
|
|
OldHam = msgpack:pack(Spam, [{spec, old}]),
|
|
|
|
{ok, Spam} = msgpack:unpack(OldHam, [{spec, old}]).
|
2013-12-14 10:46:31 +00:00
|
|
|
```
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
### `{allow_atom, none|pack}`
|
|
|
|
|
2016-02-16 09:49:34 +00:00
|
|
|
Only in packing. Atoms are packed as binaries. Default value is `pack`.
|
2016-03-20 17:02:24 +00:00
|
|
|
Otherwise, any term including atoms throws badarg.
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
### `{known_atoms, [atom()]}`
|
2014-04-09 17:57:44 +00:00
|
|
|
|
2016-02-16 08:32:35 +00:00
|
|
|
Both in packing and unpacking. In packing, if an atom is in this list
|
|
|
|
a binary is encoded as a binary. In unpacking, msgpacked binaries are
|
|
|
|
decoded as atoms with `erlang:binary_to_existing_atom/2` with encoding
|
2016-02-16 09:49:34 +00:00
|
|
|
`utf8`. Default value is an empty list.
|
2016-02-16 07:47:41 +00:00
|
|
|
|
2016-03-20 17:02:24 +00:00
|
|
|
Even if `allow_atom` is `none`, known atoms are packed.
|
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
### `{unpack_str, as_binary|as_list}`
|
2016-02-16 07:47:41 +00:00
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
A switch to choose decoded term style of `str` type when *unpacking*.
|
|
|
|
Only available at new spec. Default is `as_list`.
|
2016-02-16 08:32:35 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
mode as_binary as_list
|
|
|
|
-----------+------------+-------
|
2016-03-10 17:20:58 +00:00
|
|
|
bin binary() binary()
|
2016-02-16 08:32:35 +00:00
|
|
|
str binary() string()
|
|
|
|
```
|
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
### `{validate_string, boolean()}`
|
2016-02-16 08:32:35 +00:00
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
Only in unpacking, UTF-8 validation at unpacking from `str` type will
|
|
|
|
be enabled. Default value is `false`.
|
|
|
|
|
|
|
|
### `{pack_str, from_binary|from_list|none}`
|
|
|
|
|
|
|
|
A switch to choose packing of `string()` when packing. Only available
|
|
|
|
at new spec. Default is `from_list` for symmetry with `unpack_str`
|
|
|
|
option.
|
2014-04-09 17:57:44 +00:00
|
|
|
|
|
|
|
```
|
2016-03-10 17:20:58 +00:00
|
|
|
mode from_list from_binary none
|
|
|
|
-----------+------------+--------------+-----------------
|
|
|
|
binary() bin str*/bin bin
|
|
|
|
string() str*/array array of int array of int
|
|
|
|
list() array array array
|
|
|
|
```
|
|
|
|
|
|
|
|
But the default option pays the cost of performance for symmetry. If
|
|
|
|
the overhead of UTF-8 validation is unacceptable, choosing `none` as
|
|
|
|
the option would be the best.
|
2014-04-09 17:57:44 +00:00
|
|
|
|
2016-03-10 17:20:58 +00:00
|
|
|
- \* Tries to pack as `str` if it is a valid `string()`.
|
2016-02-16 07:47:41 +00:00
|
|
|
|
2016-05-19 04:39:17 +00:00
|
|
|
### `{map_format, map|jiffy|jsx}`
|
2016-02-16 07:47:41 +00:00
|
|
|
|
2016-05-19 04:48:03 +00:00
|
|
|
Both at packing and unpacking. Default value is `map`.
|
2014-04-09 17:57:44 +00:00
|
|
|
|
|
|
|
```erlang
|
2016-02-16 07:47:41 +00:00
|
|
|
msgpack:pack(#{ <<"key">> => <<"value">> }, []).
|
2014-04-09 17:57:44 +00:00
|
|
|
msgpack:pack({[{<<"key">>, <<"value">>}]}, [{format, jiffy}]),
|
|
|
|
msgpack:pack([{<<"key">>, <<"value">>}], [{format, jsx}]).
|
|
|
|
```
|
|
|
|
|
2016-02-16 07:47:41 +00:00
|
|
|
|
|
|
|
### `{ext, {msgpack_ext_packer(), msgpack_ext_unpacker()}|module()}`
|
|
|
|
|
2016-02-16 09:49:34 +00:00
|
|
|
At both. The default behaviour in case of facing ext data at decoding
|
|
|
|
is to ignore them as its length is known.
|
2013-12-14 06:53:13 +00:00
|
|
|
|
|
|
|
Now msgpack-erlang supports ext type. Now you can serialize everything
|
|
|
|
with your original (de)serializer. That will enable us to handle
|
|
|
|
erlang- native types like `pid()`, `ref()` contained in `tuple()`. See
|
|
|
|
`test/msgpack_ext_example_tests.erl` for example code.
|
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
2013-12-31 14:33:55 +00:00
|
|
|
Packer = fun({ref, Ref}, Opt) when is_reference(Ref) -> {ok, {12, term_to_binary(Ref)}} end,
|
|
|
|
Unpacker = fun(12, Bin) -> {ok, {ref, binary_to_term(Bin)}} end,
|
2013-12-14 10:46:31 +00:00
|
|
|
Ref = make_ref(),
|
|
|
|
Opt = [{ext,{Packer,Unpacker}}],
|
|
|
|
{ok, {ref, Ref}} = msgpack:unpack(msgpack:pack({ref, Ref}, Opt), Opt).
|
|
|
|
```
|
2013-12-14 06:53:13 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## License
|
2012-07-17 15:22:47 +00:00
|
|
|
|
|
|
|
Apache License 2.0
|
2014-04-11 00:51:24 +00:00
|
|
|
|
|
|
|
# Release Notes
|
2016-02-16 07:47:41 +00:00
|
|
|
|
|
|
|
## 0.5.0
|
|
|
|
|
2016-02-16 08:32:35 +00:00
|
|
|
- Renewed optional arguments to pack/unpack interface. This is
|
|
|
|
incompatible change from 0.4 series.
|
2014-04-11 00:51:24 +00:00
|
|
|
|
2015-12-08 06:05:57 +00:00
|
|
|
## 0.4.0
|
|
|
|
|
|
|
|
- Deprecate `nil`
|
|
|
|
- Moved to rebar3
|
2015-12-08 06:07:41 +00:00
|
|
|
- Promote default map unpacker as default format when OTP is >= 17
|
2015-12-11 02:55:05 +00:00
|
|
|
- Added QuickCheck tests
|
|
|
|
- Since this version OTP older than R16B03-1 are no more supported
|
2015-12-08 06:05:57 +00:00
|
|
|
|
2015-12-08 03:49:14 +00:00
|
|
|
## 0.3.5 / 0.3.4
|
|
|
|
|
|
|
|
- 0.3 series will be the last versions that supports R16B or older
|
|
|
|
versions of OTP.
|
|
|
|
- OTP 18.0 support
|
2015-12-08 06:07:41 +00:00
|
|
|
- Promote default map unpacker as default format when OTP is >= 18
|
2015-12-08 03:49:14 +00:00
|
|
|
|
2015-03-11 13:31:35 +00:00
|
|
|
## 0.3.3
|
|
|
|
|
|
|
|
- Add OTP 17 series to Travis-CI tests
|
|
|
|
- Fix wrong numbering for ext types
|
|
|
|
- Allow packing maps even when {format,map} is not set
|
|
|
|
- Fix Dialyzer invalid contract warning
|
|
|
|
- Proper use of null for jiffy-style encoding/decoding
|
|
|
|
|
2014-04-14 23:54:24 +00:00
|
|
|
## 0.3.2
|
2014-04-11 01:54:11 +00:00
|
|
|
|
|
|
|
- set back default style as jiffy
|
|
|
|
- fix bugs around nil/null handling
|
|
|
|
|
2014-04-11 00:51:24 +00:00
|
|
|
## 0.3.0
|
|
|
|
|
|
|
|
- supports map new in 17.0
|
|
|
|
- jiffy-style maps will be deprecated in near future
|
2014-04-11 01:54:11 +00:00
|
|
|
- set default style as map
|
2014-04-11 00:51:24 +00:00
|
|
|
|
|
|
|
## 0.2.8
|
|
|
|
|
|
|
|
0.2 series works with OTP 17.0, R16, R15, and with MessagePack's new
|
|
|
|
and old format. But does not support `map` type introduced in
|
|
|
|
OTP 17.0.
|
|
|
|
|
|
|
|
It also supports JSX-compatible mode.
|