Skip to content

Commit 542e517

Browse files
committed
add t2b/b2t-like functions
1 parent 4fab121 commit 542e517

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

src/msgpack.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
-module(msgpack).
3838

3939
-export([pack/1, unpack/1, unpack_stream/1,
40-
pack/2, unpack/2, unpack_stream/2]).
40+
pack/2, unpack/2, unpack_stream/2,
41+
term_to_binary/1, binary_to_term/1
42+
]).
4143

4244
-include("msgpack.hrl").
4345

@@ -46,6 +48,14 @@
4648
-type object() :: msgpack_term().
4749
-type options() :: msgpack_list_options().
4850

51+
-spec term_to_binary(term()) -> binary().
52+
term_to_binary(Term) ->
53+
msgpack_term:to_binary(Term).
54+
55+
-spec binary_to_term(binary()) -> term().
56+
binary_to_term(Bin) ->
57+
msgpack_term:from_binary(Bin).
58+
4959
%% @doc Encode an erlang term into an msgpack binary.
5060
%% Returns {error, {badarg, term()}} if the input is illegal.
5161
-spec pack(msgpack:object()) -> binary() | {error, {badarg, term()}}.

src/msgpack_ext.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
%% {ok, {Type, Data} = msgpack_ext_module:pack_ext(Tuple, [{ext, msgpack_ext_module}]),
2323
%% {ok, Tuple} = msgpack_ext_module:unpack_ext(Type, Data)
2424
%%
25-
-callback pack_ext(tuple(), msgpack:options()) ->
25+
-callback pack_ext(any(), msgpack:options()) ->
2626
{ok, {Type::byte(), Data::binary()}} |
2727
{error, any()}.
2828

2929
-callback unpack_ext(Type::byte(), Data::binary()) ->
30-
{ok, tuple()} | {error, any()}.
30+
{ok, any()} | {error, any()}.

src/msgpack_packer.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pack(Atom, #options_v2{allow_atom=pack} = Opt) when is_atom(Atom) ->
5050
pack(erlang:atom_to_binary(Atom, unicode), Opt);
5151

5252
%% jiffy interface
53-
pack({Map}, Opt = ?OPTION{interface=jiffy}) ->
53+
pack({Map}, Opt = ?OPTION{interface=jiffy}) when is_list(Map) ->
5454
pack_map(Map, Opt);
5555

5656
%% jsx interface
@@ -83,7 +83,7 @@ pack(List, Opt) when is_list(List) ->
8383

8484
%% Packing ext type with user defined packer function
8585
pack(Any, _Opt = ?OPTION{ext_packer=Packer, original_list=Orig})
86-
when is_tuple(Any) andalso is_function(Packer) ->
86+
when is_function(Packer) ->
8787

8888
case pack_ext(Any, Packer, Orig) of
8989
{ok, Binary} -> Binary;

src/msgpack_term.erl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
%%
2+
%% MessagePack for Erlang
3+
%%
4+
%% Copyright (C) 2009-2013 UENISHI Kota
5+
%%
6+
%% Licensed under the Apache License, Version 2.0 (the "License");
7+
%% you may not use this file except in compliance with the License.
8+
%% You may obtain a copy of the License at
9+
%%
10+
%% http://www.apache.org/licenses/LICENSE-2.0
11+
%%
12+
%% Unless required by applicable law or agreed to in writing, software
13+
%% distributed under the License is distributed on an "AS IS" BASIS,
14+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
%% See the License for the specific language governing permissions and
16+
%% limitations under the License.
17+
%%
18+
-module(msgpack_term).
19+
20+
-export([to_binary/1, from_binary/1,
21+
pack_ext/2, unpack_ext/2]).
22+
-behabiour(msgpack_ext).
23+
24+
-define(ERLANG_TERM, 131).
25+
-define(TERM_OPTION, [{enable_str,true},{ext,?MODULE},{allow_atom,none}]).
26+
27+
to_binary(Term) ->
28+
msgpack:pack(Term, ?TERM_OPTION).
29+
30+
from_binary(Bin) ->
31+
{ok, Term} = msgpack:unpack(Bin, ?TERM_OPTION),
32+
Term.
33+
34+
-spec pack_ext(tuple(), msgpack:options()) ->
35+
{ok, {Type::byte(), Data::binary()}} |
36+
{error, any()}.
37+
pack_ext(Term, _Options) ->
38+
{ok, {?ERLANG_TERM, erlang:term_to_binary(Term)}}.
39+
40+
-spec unpack_ext(Type::byte(), Data::binary()) ->
41+
{ok, any()} | {error, any()}.
42+
unpack_ext(?ERLANG_TERM, Bin) ->
43+
{ok, erlang:binary_to_term(Bin)}.
44+
45+
-ifdef(TEST).
46+
-include_lib("eunit/include/eunit.hrl").
47+
48+
test_data() ->
49+
['foobar atom', %% is_atom/1
50+
fun() -> ok end, %% is_function/1
51+
self(), %% is_pid/1
52+
%% is_port/1
53+
make_ref(), %% is_reference/1
54+
{me, foo, bar}, %% is_tuple/1
55+
{}].
56+
57+
t2b_b2t_test() ->
58+
Data = test_data(),
59+
?assertEqual(Data,
60+
msgpack:binary_to_term(msgpack:term_to_binary(Data))).
61+
62+
-endif.

0 commit comments

Comments
 (0)