mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-11-07 14:37:34 +01:00
erlang: explicit API for serializing proplists,
so as not to make wrong call of pack({proplists()}).
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
%% for C API (http://msgpack.sourceforge.jp/c:doc)
|
%% for C API (http://msgpack.sourceforge.jp/c:doc)
|
||||||
%% except buffering functions (both copying and zero-copying).
|
%% except buffering functions (both copying and zero-copying).
|
||||||
-export([pack/1, unpack/1, unpack_all/1]).
|
-export([pack/1, unpack/1, unpack_all/1]).
|
||||||
|
-export([pack_map/1]).
|
||||||
|
|
||||||
% compile:
|
% compile:
|
||||||
% erl> c(msgpack).
|
% erl> c(msgpack).
|
||||||
@@ -51,7 +52,7 @@ pack(List) when is_list(List) ->
|
|||||||
pack({Map}) when is_list(Map) ->
|
pack({Map}) when is_list(Map) ->
|
||||||
pack_map(Map);
|
pack_map(Map);
|
||||||
pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
|
pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
|
||||||
pack_map(dict:from_list(Map));
|
pack_map(dict:to_list(Map));
|
||||||
pack(_O) ->
|
pack(_O) ->
|
||||||
{error, undefined}.
|
{error, undefined}.
|
||||||
|
|
||||||
@@ -78,6 +79,15 @@ unpack_all(Data)->
|
|||||||
[Term|unpack_all(Binary)]
|
[Term|unpack_all(Binary)]
|
||||||
end.
|
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 ===== %
|
% ===== internal APIs ===== %
|
||||||
|
|
||||||
@@ -162,17 +172,6 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
|
|||||||
{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
|
{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
|
||||||
end.
|
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_([], Acc) -> Acc;
|
||||||
pack_map_([{Key,Value}|Tail], Acc) ->
|
pack_map_([{Key,Value}|Tail], Acc) ->
|
||||||
pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
|
pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
|
||||||
@@ -404,7 +403,7 @@ unknown_test()->
|
|||||||
-234.4355, 1.0e-34, 1.0e64,
|
-234.4355, 1.0e-34, 1.0e64,
|
||||||
[23, 234, 0.23],
|
[23, 234, 0.23],
|
||||||
[0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
|
[0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
|
||||||
dict:from_list([{1,2},{<<"hoge">>,nil}]),
|
{[{1,2},{<<"hoge">>,nil}]},
|
||||||
-234, -50000,
|
-234, -50000,
|
||||||
42
|
42
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user