2013-12-14 10:46:31 +00:00
|
|
|
# MessagePack Erlang
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
![Travis](https://secure.travis-ci.org/msgpack/msgpack-erlang.png)
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
![Drone.io](https://drone.io/github.com/msgpack/msgpack-erlang/status.png)
|
2013-09-10 15:55:00 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## prequisites for runtime
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2015-12-08 06:05:57 +00:00
|
|
|
[Erlang/OTP](http://erlang.org/), >= 17.0
|
2013-12-14 10:46:31 +00:00
|
|
|
Also based on [the new msgpack spec 232a0d](https://github.com/msgpack/msgpack/blob/232a0d14c6057000cc4a478f0dfbb5942ac54e9e/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
|
|
|
|
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
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## String type
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
Now this supports string type!
|
2012-07-17 15:22:47 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
|
|
|
Opt = [{enable_str, true}]
|
|
|
|
{ok, "埼玉"} = msgpack:unpack(msgpack:pack("埼玉", Opt), Opt).
|
|
|
|
=> {ok,[22524,29577]}
|
|
|
|
```
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
There are several options for `msgpack:pack/2` and `msgpack:unpack/2` .
|
|
|
|
See `msgpack:options()` in `msgpack.hrl`.
|
2013-02-03 12:01:11 +00:00
|
|
|
|
2014-04-09 17:57:44 +00:00
|
|
|
## Map Style
|
|
|
|
|
|
|
|
Since Erlang/OTP 17.0
|
|
|
|
|
|
|
|
```erlang
|
|
|
|
msgpack:pack(#{ <<"key">> => <<"value">> }, [{format, map}]).
|
|
|
|
```
|
|
|
|
|
|
|
|
Or use old jiffy/jsx style
|
|
|
|
|
|
|
|
```erlang
|
|
|
|
msgpack:pack({[{<<"key">>, <<"value">>}]}, [{format, jiffy}]),
|
|
|
|
msgpack:pack([{<<"key">>, <<"value">>}], [{format, jsx}]).
|
|
|
|
```
|
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## Ext type
|
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
|
|
|
|
|
|
|
This is still experimental feature, so I'm waiting for your feedback.
|
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
## Compatibility mode
|
2013-09-20 15:30:42 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
To use as same with [old spec](https://github.com/msgpack/msgpack/blob/master/spec-old.md):
|
2013-09-20 15:30:42 +00:00
|
|
|
|
2013-12-14 10:46:31 +00:00
|
|
|
```erlang
|
|
|
|
OldHam = msgpack:pack(Spam, [{enable_str,false}]),
|
|
|
|
{ok, Spam} = msgpack:unpack(OldHam, [{enable_str,false}]).
|
|
|
|
```
|
2013-09-20 15:30:42 +00:00
|
|
|
|
2013-09-25 01:02:22 +00:00
|
|
|
Since 0.2.3 now it's **false by default**.
|
|
|
|
|
2013-12-25 15:05:57 +00:00
|
|
|
## Further tests
|
2013-09-25 01:02:22 +00:00
|
|
|
|
2013-12-25 15:05:57 +00:00
|
|
|
See [msgpack-erlang-tests](http://github.com/kuenishi/msgpack-erlang-tests) for further tests
|
2012-07-17 15:22:47 +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
|
|
|
|
|
2015-12-08 06:05:57 +00:00
|
|
|
## 0.4.0
|
|
|
|
|
|
|
|
- Deprecate `nil`
|
|
|
|
- Moved to rebar3
|
|
|
|
|
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-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.
|