mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-17 15:14:49 +02:00
erlang: explicit API for serializing proplists,
so as not to make wrong call of pack({proplists()}).
This commit is contained in:
parent
9b5fc37399
commit
90e305d789
@ -24,6 +24,7 @@
|
||||
%% for C API (http://msgpack.sourceforge.jp/c:doc)
|
||||
%% except buffering functions (both copying and zero-copying).
|
||||
-export([pack/1, unpack/1, unpack_all/1]).
|
||||
-export([pack_map/1]).
|
||||
|
||||
% compile:
|
||||
% erl> c(msgpack).
|
||||
@ -51,7 +52,7 @@ pack(List) when is_list(List) ->
|
||||
pack({Map}) when is_list(Map) ->
|
||||
pack_map(Map);
|
||||
pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
|
||||
pack_map(dict:from_list(Map));
|
||||
pack_map(dict:to_list(Map));
|
||||
pack(_O) ->
|
||||
{error, undefined}.
|
||||
|
||||
@ -78,6 +79,15 @@ unpack_all(Data)->
|
||||
[Term|unpack_all(Binary)]
|
||||
end.
|
||||
|
||||
pack_map(M)->
|
||||
case length(M) of
|
||||
Len when Len < 16 ->
|
||||
<< 2#1000:4, Len:4/integer-unit:1, (pack_map_(M, <<>>))/binary >>;
|
||||
Len when Len < 16#10000 -> % 65536
|
||||
<< 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>;
|
||||
Len ->
|
||||
<< 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>
|
||||
end.
|
||||
|
||||
% ===== internal APIs ===== %
|
||||
|
||||
@ -162,17 +172,6 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
|
||||
{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
|
||||
end.
|
||||
|
||||
% FIXME: write test for pack_map/1
|
||||
pack_map(M)->
|
||||
case length(M) of
|
||||
Len when Len < 16 ->
|
||||
<< 2#1000:4, Len:4/integer-unit:1, (pack_map_(M, <<>>))/binary >>;
|
||||
Len when Len < 16#10000 -> % 65536
|
||||
<< 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>;
|
||||
Len ->
|
||||
<< 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>
|
||||
end.
|
||||
|
||||
pack_map_([], Acc) -> Acc;
|
||||
pack_map_([{Key,Value}|Tail], Acc) ->
|
||||
pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
|
||||
@ -404,7 +403,7 @@ unknown_test()->
|
||||
-234.4355, 1.0e-34, 1.0e64,
|
||||
[23, 234, 0.23],
|
||||
[0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
|
||||
dict:from_list([{1,2},{<<"hoge">>,nil}]),
|
||||
{[{1,2},{<<"hoge">>,nil}]},
|
||||
-234, -50000,
|
||||
42
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user