Skip to content

Commit f23e3f2

Browse files
committed
Fix for known_atoms decoding
1 parent 72dfb8c commit f23e3f2

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/msgpack.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ unpack_stream(Bin) -> unpack_stream(Bin, []).
129129
unpack_stream(Bin, Opts0) when is_binary(Bin) ->
130130
Opts = parse_options(Opts0),
131131
try
132-
msgpack_unpacker:unpack_stream(Bin, Opts)
132+
Opts1=Opts?OPTION{
133+
known_atoms=lists:map(
134+
fun(X) ->
135+
erlang:atom_to_binary(X, utf8)
136+
end,
137+
Opts?OPTION.known_atoms
138+
)
139+
},
140+
msgpack_unpacker:unpack_stream(Bin, Opts1)
133141
catch
134142
throw:Exception -> {error, Exception}
135143
end;

src/msgpack_unpacker.erl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ unpack_str_or_raw(V, ?OPTION{spec=new,
237237
as_tagged_list -> {string, unpack_str(V)}
238238
end, Rest}.
239239

240+
maybe_bin(Bin, ?OPTION{known_atoms=NA}) when NA=/=[] ->
241+
case lists:member(Bin,NA) of
242+
true ->
243+
erlang:binary_to_existing_atom(Bin,utf8);
244+
false ->
245+
Bin
246+
end;
247+
240248
maybe_bin(Bin, _) ->
241249
Bin.
242250

test/msgpack_tests.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ binary_test_() ->
379379
end}
380380
].
381381

382+
atom_test_() ->
383+
Map1=#{atom1=>atom1,atom2=><<"binary2">>},
384+
Bin=msgpack:pack(Map1),
385+
?_assertEqual({ok,#{<<"atom1">>=><<"atom1">>,
386+
<<"atom2">>=><<"binary2">>}}, msgpack:unpack(Bin)),
387+
?_assertEqual({ok,Map1}, msgpack:unpack(Bin,[{known_atoms,[atom1,atom2]}])).
388+
382389
-define(PCNT, 5).
383390
-define(CNT, 10000).
384391

0 commit comments

Comments
 (0)